Eight years ago, Little B saw an osmanthus tree, which is a tree with vertices, where the parent vertex of any non-root vertex in has a smaller label than its own. Given an integer , a rooted tree with vertices is prosperous if and only if the following conditions are met:
- For any with , the lowest common ancestor of vertices and in and has the same label.
- For any with , the label of the lowest common ancestor of vertices and in does not exceed .
Note that all vertices in the trees are labeled starting from , and the label of the root vertex is . does not need to satisfy the condition that the parent vertex of any non-root vertex has a smaller label than its own.
Little B wants to know how many trees with vertices are prosperous. Two trees are considered different if there exists a vertex whose parent vertex is different in the two trees. Output the number of solutions modulo .
Input Specification
This problem has multiple test data sets.
The first line of the input contains two integers and , which represent the test case number and the number of test data sets. represents that this test case is a sample test.
Then, each set of test data is given as input in order. For each set of test data:
The first line of the input contains three integers , , and .
The second line of the input contains integers , where represents the label of the parent vertex of vertex in .
Output Specification
For each set of test data, output a line containing an integer, representing the number of possible prosperous trees, taken modulo .
Sample Input 1
0 3
1 2 1
2 2 1
1
2 2 0
1
Sample Output 1
3
16
15
Explanation for Sample Output 1
For the first set of test data in the sample, there are three valid trees, with parent sequences of being , , and , respectively. Note that the second line in this set of test data is blank.
For the second and third sets of test data in the sample, there are a total of trees that satisfy the first condition. Among them, only the tree with parent sequence does not satisfy the second condition in the third set of test data.
Additional Samples
Sample inputs and outputs can be found here.
- Sample 2 (
ex_tree2.in
andex_tree2.ans
) satisfies , and for the five sets of test data, does not exceed , respectively. - Sample 3 (
ex_tree3.in
andex_tree3.ans
) satisfies . The first two sets of test data satisfy , and the first, third, and fourth out of the five sets of test data satisfy . - Sample 4 (
ex_tree4.in
andex_tree4.ans
). The first two sets of test data of this sample satisfy , and the first, third, and fourth sets of test data satisfy .
Problem Constraints
For all test data, it is guaranteed that: .
Test ID | |||
---|---|---|---|
Comments