Glass Balls
题号:NC225775
时间限制:C/C++/Rust/Pascal 2秒,其他语言4秒
空间限制:C/C++/Rust/Pascal 512 M,其他语言1024 M
64bit IO Format: %lld

题目描述

Here you are given a 1-rooted tree with n vertices, with a glass ball staying on every vertex, respectively.

For every vertex u with parent v, u's altitude (namely height) is higher than v by 1 unit. Thus a ball on u can roll along edge (u, v) to v. A ball rolling along an edge takes 1 second.

All balls start rolling at the same time. There are some special vertices called ``storage vertices''. Not every vertex is storable. Besides the root, which must be storable, the probability of a vertex being storable is p. If a ball rolls to a storage vertex, it breaks, and it will be taken up from the tree. If a ball is initially on a storage vertex, it breaks immediately.

Two balls rolling to the same vertex at the same time is called a collision, without considering the type of the vertex. If collision occurs, it will be a terrible incident. Not only the two balls will break, but also the whole system, and all the rolling cannot continue any more.

If collisions occur, you lose and your score is 0. Otherwise your score is , where f(u) denotes the number of edges that the ball initially on u come across during the whole rolling process. Now please calculate the expect score module .

输入描述:

The first line contains two integers n, p (, ), where p is the probability modulo . To explain this, we denote p as an irreducible fraction , where a and b are integers and . Then . In other words .

The second line contains n - 1 space-separated integers , where a_i denotes the parent vertex of i.

输出描述:

Output one line contains one integer denoting the expect score.
示例1

输入

复制
3 499122177
1 1

输出

复制
499122177
示例2

输入

复制
8 1919810
1 1 1 4 5 1 4

输出

复制
495380040