##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
You are given a rooted tree with vertices, numbered from to . The root is vertex .
For every vertex with , you are given its parent . It is guaranteed that . The parents are given in one line.
At first, only vertex is red, and all other vertices are white. Vertex always remains red.
You are given queries. The query vertices are given in a single line. In each query, a vertex with is given. If vertex is white, it becomes red. If vertex is red, it becomes white.
After each query, consider all ways to choose some of the white vertices and color them blue. If there are white vertices, there are such choices.
For each choice, define the colored vertices as all vertices that are red or blue. The blue coloring is only used for this calculation and is not kept for later queries.
The beauty of the tree is the minimum number of edges that must be marked so that, using only marked edges, it is possible to move between every pair of colored vertices. If there is only one colored vertex, the beauty is .
After each query, output the sum of the beauties over all choices of blue vertices, modulo .
There is only one non-root vertex. After each query, either it is red or it is the only vertex that may become blue, so the sum is always .
After vertex becomes red, choosing vertex as blue or not gives beauties and . After vertex also becomes red, both edges are needed. After vertex becomes white again, the path between vertices and still uses both edges in every blue choice.
The tree is a star centered at vertex . The beauty is exactly the number of colored leaves, so each query changes which leaves are already forced to be colored.
After the first query, vertex is red. For example, if neither vertex nor vertex is blue, the marked edges are the two edges on the path from to . Summing over all blue choices gives the printed values.
In the last query, vertex becomes red. Then vertices are red, and vertices are white. The four possible blue choices have beauties , so the sum is .