#P9884. Expectation of Rank

Expectation of Rank

Expectation of Rank

Problem Description

Let pp be a prime number and Fp\mathbb{F}_p be the finite field with order pp. Suppose AA is a square matrix of order nn and each of its entry is a random variable that uniformly distributed on Fp\mathbb{F}_p. Please calculate the expectation of the rank of AA, i.e., E[rank(A)]\mathbb{E}[\mathrm{rank}(A)]. In mathematics, a finite field, also known as a Galois field, is a set that contains a finite number of elements. These elements follow the operations of multiplication, addition, subtraction, and division, all of which satisfy the basic rules of arithmetic. The most common examples of finite fields are given by the integers mod pp when pp is a prime number. When we say Fp\mathbb{F}_p is a finite field with order pp, it means that Fp\mathbb{F}_p contains exactly pp distinct elements, with pp being a prime number. The elements of Fp\mathbb{F}_p are the integers 0,1,2,...,p10, 1, 2, ..., p-1, and the operations of the field are performed modulo pp. For instance, if p=5p=5, then F5\mathbb{F}_5 is the set {0,1,2,3,4}\{0, 1, 2, 3, 4\}, and in this field, 2+3=02+3=0 and 4×4=14 \times 4=1.

Input

The first line of the input contains an integer TT (1T501 \leq T \leq 50), denoting the number of test cases. Each of the following TT lines contains two integers n,pn, p (1n50001 \leq n \leq 5000, 2p1092 \leq p \leq 10^9), denoting the order of the square matrix AA and the prime number pp. It's guaranteed that the sum of nn in all test cases will not exceed 50005000.

Output

For each test case, output the expectation of the rank of AA. You should output the answers modulo 109+710^9+7. That is, if the answer is PQ\frac{P}{Q}, you should output PQ1mod109+7P\cdot Q^{-1}\bmod 10^9+7, where Q1Q^{-1} denotes the multiplicative inverse of QQ modulo 109+710^9+7. It can be proved that the answer can always be expressed in this form.

Sample Input

2
2 2
2 3

Sample Output

812500007
802469143

Hint

The rank of the matrix

[1221]\begin{bmatrix} 1 & 2\newline 2 & 1 \end{bmatrix}

in F3\mathbb{F}_3 is 11.

Source

2023“钉耙编程”中国大学生算法设计超级联赛(8)