Roxanne the mage, after countless hours researching ancient arcana, has decided to go to the local cafe to relax. When she arrived at the old cafe, she saw a strange structure on the wall called an arboras (or tree). Formally, it's a collection of vertices numbered with consecutive non-negative integers, where vertex is the root, and all other vertices have a unique parent (vertex has parent ). Since the cafe is run by mages and programmers collectively, the arboras (or tree) is drawn with the root at the top.
The mage is intrigued by this structure, and she decides to pour some magic coffee into one of the vertices. If the coffee is poured into vertex , then it will flow downwards, through the subtree rooted at vertex . Since it is magic coffee, it doesn't flow randomly: it occupies the longest chain it can possibly occupy, within the subtree rooted at , while passing through the vertex . The amount of coffee lost when pouring is proportional to the length of the chain the coffee occupies. Roxanne denotes this amount as . Note that individual edges of the tree might have different lengths.
Task
Roxanne is interested in the amount of coffee she would lose if she poured it in all the vertices of the tree, that is, the sum of over all vertices of the tree. This is not difficult to compute at first, but then the programmers decide to challenge her, and increase the length of certain edges times. Can you help Roxanne find the total length of all the chains occupied by the coffee if poured into all vertices, initially and after each of the updates? Beware! She needs the answers modulo .
Input
The first line contains integer , the number of vertices.
The second line contains integers: , where is the parent of vertex , while vertex is the root.
The third line contains integers: , where is the length of the edge between vertices and .
The fourth line contains , the number of updates.
Each of the next lines contain two integers and , representing the update: the length of the edge between vertices and is increased by .
Output
Print lines: on the line you should print the answer after the update. On the first line you should print the answer before any updates.
All answers must be printed modulo .
Constraints
- for all
- for all
# | Points | Constraints |
---|---|---|
1 | 11 | |
2 | 13 | The height of the tree is at most . |
3 | 31 | for all for all |
4 | 45 | No additional constraints. |
Examples
stdin
5
0 0 1 1
0 0 0 0
10
1 2
2 2
3 2
4 2
4 1
3 1
2 1
1 1
4 1000
2 1000
stdout
0
2
4
8
10
12
13
14
15
2015
3015