#P10103. Derangements with Restricted First Elements

    ID: 12088 Type: Default 1000ms 256MiB

Derangements with Restricted First Elements

Derangements with Restricted First Elements

You are given an integer T representing the number of test cases. For each test case, you are given two integers n and m.

Consider a permutation p = [p1, p2, ..., pn] of the set \(\{1, 2, \dots, n\}\). We say that p is valid if it satisfies both of the following conditions:

  • For every index \(i\) with \(1 \le i \le m\), \(p_i > m\). (In other words, the first m positions cannot contain numbers from \(\{1, 2, \dots, m\}\).)
  • For every index \(i\) (\(1 \le i \le n\)), \(p_i \ne i\). (The permutation is a derangement.)

Note that for the first m positions, the condition \(p_i \ne i\) is automatically true since \(i \le m\) and \(p_i > m\). However, the derangement condition must be ensured for the remaining indices.

Important: In order for the first condition to be possible, there must be at least m numbers larger than m in \(\{1,2,\dots,n\}\); that is, \(n - m \ge m\) or equivalently \(n \ge 2m\). If \(n < 2m\), the answer is 0.

The answer for each test case may be very large, so you are required to compute it modulo \(998244353\).

Input/Output Format: See the sections below.

inputFormat

The input begins with an integer T indicating the number of test cases. Each of the following T lines contains two space-separated integers n and m.

outputFormat

For each test case, output a single integer, the number of valid permutations modulo 998244353.

sample

3
4 1
4 2
3 2
9

4 0

</p>