D. Token Removing

Problem Statement

Define a sequence of integers aa valid if and only if 1in,0aii∀1 ≤ i ≤ n, 0 ≤ a_i ≤ i.

Define the weight f(a)f(a) of a valid sequence aa of length nn:

  • Initially, a token is placed at each integer point in the closed segment [1,n][1, n] on the number axis.
  • Perform nn operations in sequence. During the ii-th operation, if ai0a_i ≠ 0, remove a token in the closed segment [ai,i][a_i, i] that has not been removed; otherwise, do nothing.
  • f(a)f(a) is the number of ways to remove tokens. Two ways are considered different if there exists a tt such that the positions of the tokens removed by the two ways are different at the tt-th operation.
  • For example, f([0,2,1])=2f([0,2,1]) = 2, because we can remove tokens on 2,12,1 or 2,32,3 in sequence.

JT gives you two integers n,mn, m and asks you to find the sum of the weights over all (n+1)!(n+1)! valid sequences of length nn. Since the answer may be too large, print it modulo mm.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t10001 ≤ t ≤ 1000). The description of the test cases follows.

The only line of each test case contains two integers nn and mm (1n50001 ≤ n ≤ 5000, 108m1.01×10910^8 ≤ m ≤ 1.01 × 10^9) — the length of valid sequences, and the modulus.

It is guaranteed that the sum of n2n^2 over all test cases does not exceed 2.5×1072.5 × 10^7.

Output

For each test case, output an integer — the sum of the weights over all (n+1)!(n+1)! valid sequences of length nn, modulo mm.

Solution

The goal is to calculate the sum of weights f(a) over all possible valid sequences a, modulo m. The weight f(a) is the number of ways to remove tokens based on the sequence a.

1. The Core Idea: Changing Perspective

Instead of iterating through every possible sequence a and calculating its contribution, the solution flips the problem on its head. We ask a different question:

For a specific set of token positions P that are removed, what is its total contribution to the final answer?

If we can calculate this for any given set P, we can then sum these contributions over all possible sets P to get the final answer.

2. Contribution of a Single Set of Tokens P

Let’s assume we decide to remove a specific set of kk tokens located at positions P=p0,p1,...,pk1P = {p_0, p_1, ..., p_{k-1}}. For convenience, let’s sort them in descending order: np0>p1>...>pk11n ≥ p_0 > p_1 > ... > p_{k-1} ≥ 1.

For this set P to be removed, we need to find a sequence aa and a corresponding removal plan. This involves two main parts:

2.1 Choosing a[t]a[t] values

For each token pjp_j that is removed at some step tjt_j, the condition is a[tj]pjtja[t_j] ≤ p_j ≤ t_j. The value a[tj]a[t_j] can be any integer from 1 to pjp_j. This gives pjp_j choices for a[tj]a[t_j]. For all kk tokens, the total number of choices for the aa values is the product p0×p1×...×pk1p_0 × p_1 × ... × p_{k-1}.

2.2 Assigning removal steps tjt_j

We need to assign a unique removal step tjt_j to each token pjp_j such that tjpjt_j ≥ p_j.

  • For the largest token p0p_0, it can be removed at any step from p0p_0 to nn. There are np0+1n - p_0 + 1 available steps.
  • For the second largest, p1p_1, it can be removed at any step from p1p_1 to nn, except for the step already taken by p0p_0. Since p0>p1p_0 > p_1, the step for p0p_0 is always a valid (but unavailable) step for p1p_1. This leaves (np1+1)1(n - p_1 + 1) - 1 choices.
  • Generalizing, for the token pjp_j (which is the jj-th largest, with jj starting from 0), there are npj+1n - p_j + 1 potential steps. However, jj of these steps have already been assigned to the jj tokens larger than pjp_j. This leaves (npj+1j)(n - p_j + 1 - j) choices for tjt_j.

2.3 Final Contribution Formula

Combining these two parts, the total contribution for a single, fixed set P is:

Contribution(P)=(j=0k1pj)×(j=0k1(npj+1j))=j=0k1pj(npj+1j)\text{Contribution}(P) = \left(\prod_{j=0}^{k-1} p_j\right) \times \left(\prod_{j=0}^{k-1} (n - p_j + 1 - j)\right) = \prod_{j=0}^{k-1} p_j(n - p_j + 1 - j)

Our new goal is to sum this value over all possible non-empty subsets PP of 1,2,...,n{1, 2, ..., n}.

3. The Dynamic Programming Solution

Enumerating all 2n2^n subsets P is too slow. We can use dynamic programming to calculate this sum efficiently. The key is to build the sets P one element at a time.

The crucial insight is to consider elements in descending order, from nn down to 11.

3.1 DP State Definition

Let’s define our DP state:

dp[i][k]\text{dp}[i][k] = The sum of contributions for all sets PP of size kk where all chosen tokens are from the range i,i+1,...,n{i, i+1, ..., n}

3.2 DP Transition

We compute this by iterating ii from nn down to 11. For each ii, we make a decision:

  1. Don’t include ii in our set P: If we don’t choose ii, we must still choose kk tokens, but now they must all come from the range i+1,...,n{i+1, ..., n}. The sum of contributions for this case is, by definition, dp[i+1][k]\text{dp}[i+1][k].

  2. Include ii in our set P: If we choose ii, we need to select k1k-1 other tokens from the range i+1,...,n{i+1, ..., n}.

    • The sum of contributions for all such sets of k1k-1 tokens from i+1,...,n{i+1, ..., n} is dp[i+1][k1]\text{dp}[i+1][k-1].
    • Now, we add ii to each of these sets. Since ii is smaller than any number in i+1,...,n{i+1, ..., n}, it will always be the smallest element in the new kk-element set. This means its rank (from largest, 0-indexed) will always be k1k-1.
    • The multiplicative factor for ii is therefore i×(ni+1(k1))i × (n - i + 1 - (k-1)).
    • The total contribution for this case is dp[i+1][k1]×i×(ni+2k)\text{dp}[i+1][k-1] × i × (n - i + 2 - k).

3.3 The DP Recurrence

Combining these cases, we get the recurrence:

dp[i][k]=dp[i+1][k]+dp[i+1][k1]×i×(ni+2k)\text{dp}[i][k] = \text{dp}[i+1][k] + \text{dp}[i+1][k-1] × i × (n - i + 2 - k)

Base Cases:

  • dp[n+1][0]=1\text{dp}[n+1][0] = 1 (representing one way to choose an empty set)
  • dp[n+1][k]=0\text{dp}[n+1][k] = 0 for k>0k > 0

Final Answer: The final answer is the sum of contributions for all possible set sizes kk, considering all numbers from 1 to nn. This is k=0ndp[1][k]\sum_{k=0}^{n} \text{dp}[1][k].

submission link