| Codeforces Round 1035 (Div. 2) |
|---|
| Finished |
Define a sequence of integers $$$a$$$ valid if and only if $$$\forall 1 \le i \le n, 0 \le a_i \le i$$$.
Define the weight $$$f(a)$$$ of a valid sequence $$$a$$$ of length $$$n$$$:
For example, $$$f([0, 2, 1]) = 2$$$, because we can remove tokens on $$$2, 1$$$ or $$$2, 3$$$ in sequence.
JT gives you two integers $$$n, m$$$ and asks you to find the sum of the weights over all $$$(n + 1)!$$$ valid sequences of length $$$n$$$. Since the answer may be too large, print it modulo $$$m$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). The description of the test cases follows.
The only line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 5000, 10^8 \le m \le 1.01 \cdot 10^9$$$) — the length of valid sequences, and the modulus.
It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$2.5 \cdot 10^7$$$.
For each test case, output an integer — the sum of the weights over all $$$(n + 1)!$$$ valid sequences of length $$$n$$$, modulo $$$m$$$.
61 10000000072 10000000073 10000000074 10000000075 1000000007114 514191981
2 7 37 273 2672 393775292
In the first test case, valid sequences are $$$[0]$$$ and $$$[1]$$$, and the answer is $$$f([0]) + f([1]) = 1 + 1 = 2$$$.
In the second test case, valid sequences are $$$[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2]$$$. The weight of $$$[0, 1]$$$ is $$$2$$$, and the others are $$$1$$$, so the answer is $$$5 \cdot 1 + 1 \cdot 2 = 7$$$.
| Name |
|---|


