Meow
시간 제한0.5초메모리 제한1024 MB
루트 있는 트리에서 값을 한 점씩 Q번 바꾸면서, 값이 1부터 L까지 순서대로 늘어선 조상 사슬의 개수를 세고 그 개수들의 가중 합을 구한다.
문제
In the CS club there's a new Pokemon, Meow2. Being passionate about trees, Meow2 has a rooted tree with nodes, labeled from to . Node is the root of the tree, and every for every other node its father has a label smaller than . Each node has an associated value, an integer between and .
Meow2 also has an array of length . He wants to know the number of occurrences of in the tree. More exactly, he wants to count the number of sequences such that the value associated with node , and for each node is an ancestor of node .
Being an ever evolving Pokemon, Meow2 keeps changing the initial tree. He has a magical array of changes, , of length . At each step , , he changes the value of node to , .
Meow2 would like to know after each change the number of occurrences of in the tree, as defined above. If we denote by the number of occurrences of after the th change, you should find:
입력
The first line contains integers , and .
The second line contains an array of length , where is the father of node .
The third line contains an array of length , representing the initial values of the nodes.
The next lines contains an integer each, representing the changes made on the tree.
출력
Output a single integer modulo .
제한
힌트
The individual answers are:
Below you can see the tree after the first update
