Counting Heaps

No attempts yetTime limit5sMemory limit128 MB

Problem

You are given a rooted tree with nn vertices. Assign the numbers 11 through nn to the vertices, using each number exactly once, so that every vertex receives a number smaller than its parent's number. In other words, the root holds the largest number and the numbers decrease from parents to children, forming a max-heap.

Count the number of such labelings. Since this number can be very large, print it modulo mm.

Input

The first line contains the number of test cases tt. (1t2501 \le t \le 250)

The first line of each test case contains the number of vertices nn and the modulus mm. (1n500,0001 \le n \le 500{,}000, 2m1092 \le m \le 10^{9})

The ii-th of the next n1n-1 lines contains pi+1p_{i+1}, the parent of vertex i+1i+1. (1pi+1i1 \le p_{i+1} \le i) Vertex 1 is always the root. The total input size does not exceed 50MB.

Output

For each test case, print the number of valid labelings modulo mm, one per line.

Hint

For example, in the last case of the sample (n=5n=5, where vertices 2,3,4,52,3,4,5 have parents 1,1,3,31,1,3,3), there are exactly 88 valid labelings.

Note that mm is not necessarily prime, so you cannot directly divide (use a modular inverse) under the modulus.