Tree of Almost Clean Money

Each operation adds generated values to up to 1000 vertices and asks for the sum on the path between two vertices.

Medium7TreeSegment treeNo attempts yetTime limit4sMemory limit256 MB

Problem

Money grows on a tree with NN vertices. The vertices are numbered 00 to N1N-1 and vertex 00 is the root. Every vertex ii other than vertex 00 has a parent p(i)p(i) with p(i)<ip(i) < i. At the start, vertex ii holds v(i)v(i) monetary units.

An organization runs QQ operations on the tree. Each operation has two steps.

  1. Pick KK vertices x(1),,x(K)x(1), \dots, x(K) from the tree (0x(i)N10 \le x(i) \le N-1). The same vertex may be picked more than once. For each ii with 1iK1 \le i \le K, add y(i)y(i) monetary units to vertex x(i)x(i).
  2. Pick two vertices uu and vv (0u,vN10 \le u, v \le N-1). Compute the total amount of money held by the vertices on the unique path between uu and vv, with uu and vv included.

Compute the answer to step 2 of every operation.

The limits are as follows.

  • 1N5000001 \le N \le 500000
  • 1Q500001 \le Q \le 50000
  • 1K10001 \le K \le 1000
  • 0v(i)<10000000070 \le v(i) < 1000000007
  • 0y(i)<10000000070 \le y(i) < 1000000007

Input

The first line contains the number of vertices NN. Each of the next N1N-1 lines contains two space separated integers p(i)p(i) and ii describing one edge of the tree. The next line contains the starting amounts v(0),,v(N1)v(0), \dots, v(N-1), separated by spaces.

The next line contains the number of operations QQ. Each of the next QQ lines describes one operation with nine space separated integers in this order: KK, x(1)x(1), y(1)y(1), AA, BB, CC, DD, uu, vv (0A,B,C,D<10000000070 \le A, B, C, D < 1000000007). Only x(1)x(1) and y(1)y(1) are given directly. The remaining values for 2iK2 \le i \le K come from

x(i)=(Ax(i1)+B)modNx(i) = (A \cdot x(i-1) + B) \bmod N

y(i)=(Cy(i1)+D)mod1000000007y(i) = (C \cdot y(i-1) + D) \bmod 1000000007

Output

Print QQ lines. Line jj contains the answer to step 2 of operation jj. Money added in step 1 stays in the vertex, so every operation computes its answer with the step 1 additions of all earlier operations and of its own step 1 already applied.

An answer can exceed the range of a 32-bit integer.

Hint

In the first example, operation 1 has A=C=1A = C = 1 and B=D=0B = D = 0, so the value 11 is added to vertex 11 one thousand times. The path between vertex 00 and vertex 22 passes through vertices 00, 11, 22, and the total amount of money in them is 10061006.

In operation 2 the generated values are x(1)=0x(1)=0, y(1)=5y(1)=5, x(2)=1x(2)=1, y(2)=12y(2)=12, and the path between vertex 22 and vertex 33 passes through every vertex of the tree.

In operation 3, K=1K = 1, so AA, BB, CC, DD are not used.