JOJO's Happy Tree Friends
题号:NC237068
时间限制:C/C++/Rust/Pascal 1秒,其他语言2秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

Joseph is playing a game with his friends. The game goes on a tree -- a connected graph with n vertices and n - 1 edges. The vertices are labeled by . The root of the tree is 1.
There is a token initially on one of the vertices. At each step, Joseph will randomly choose a vertex from the n vertices with equal probability, and then move the token according to the rules as follows:
Assume the token is currently on vertex u. And then vertex v is selected by Joseph at random
  • If u is an ancestor of v, move the token to the vertex v.
  • If u is not an ancestor of v, move the token to the vertex . denotes the lowest common ancestor of vectex u and v.
There is also a target vertex w in the tree. As soon as the token is moved to the target vertex, the game is over. Otherwise, the game will keep going until it hits the target vertex.
He wants to know the expected number of steps to hit the target when the token starts from each vertex.
Let's denote the expectation of steps starting from vertex u modulo by E(u). You are only expected to print

输入描述:

The first line contains one integer , indicating the number of vertices.
The following line contains n-1 integers p_2, p_3, ...., p_n, indicating the parent of each vertex.
The third contains one integer , indicating the target vertex.

输出描述:

Only one integer -- the answer after encrypting.
示例1

输入

复制
4
1 2 1
1

输出

复制
333333343

说明

The expected number of steps to hit the target starting from vectices 1,2,3,4 are 0, 2, 2, \frac{4}{3} \equiv 333333337 respectively.
示例2

输入

复制
6
1 2 3 4 5
5

输出

复制
23

说明

For the second example, the expectations are 6,6,6,6,0,6 respectively.
示例3

输入

复制
10
1 2 2 1 5 6 6 8 8
8

输出

复制
3263736426