2740. Counting heaps

单点时限: 10.0 sec

内存限制: 256 MB

We are given a rooted tree of n vertices. The vertices are to be labeled with numbers 1,2,…,n so that each label is unique and the heap condition holds, i.e. the label of any vertex is less than the label of its parent. How many such labellings exist? Since this number may be quite large, calculate only its remainder modulo m.

输入格式

The input contains several tree descriptions. The first line contains the number of input trees t (t <= 250). Each tree description begins with a line containing the size of the tree n (1 <= n <= 500000) and an integer m (2 <= m <= 109). n-1 lines follow, i-th of which contains p(i + 1), the number of the parent of the (i + 1)-th vertex (1 <= p(i + 1) <= i). Vertex number 1 will be the root in each tree, so its parent will not be given. Total size of the input will not exceed 50MB.

输出格式

For each tree output the number of its valid labellings modulo given m.

样例

Input
4
3 1000000
1
1
4 1000000
1
1
1
5 1000000
1
2
3
4
5 1000000
1
1
3
3
Output
2
6
1
8

0 人解决,3 人已尝试。

0 份提交通过,共有 10 份提交。

9.9 EMB 奖励。

创建: 11 年,11 月前.

修改: 4 年前.

最后提交: 11 年,9 月前.

来源: Central European Programming Contest 2008

题目标签