#P9099. [PA 2020] Ogromne drzewo

[PA 2020] Ogromne drzewo

Problem Description

This problem is translated from PA 2020 Round 3 Ogromne drzewo.

Byteasar bought his girlfriend Algolina a huge Christmas tree. This is a very unusual gift, but Byteasar is an algorithmist, and Algolina is already used to such surprises.

As you may have guessed, this tree is not a plant, but a connected acyclic graph. It is very large, but it can be described in an organized way. Its vertices have nn levels. The first level contains only one vertex, which is the root of the tree. The children of each vertex are only on the next level, except for the vertices on the last level, which are leaves. For each ii in the range [1,n1][1, n - 1], every vertex on level ii has aia_i children.

Algolina wants Byteasar to know how satisfied she is with the gift, so she decides to play a game with him. Algolina chooses a vertex AA in the tree, and Byteasar chooses a vertex BB (possibly the same as Algolina’s). Now, starting with Algolina, they will take turns recoloring vertices of the tree: Algolina uses red, and Byteasar uses blue. At the beginning of the game, all vertices are white. Each vertex will be recolored exactly once, either by Algolina or by Byteasar. At any time, the player whose turn it is may recolor any white vertex with their own color, including vertex AA and vertex BB.

Once all vertices have been recolored, they compute their scores. Algolina’s score (denoted by SAS_A) is the sum of distances from all red vertices to vertex AA, and Byteasar’s score (denoted by SBS_B) is the sum of distances from all blue vertices to vertex BB. The distance between two vertices is the number of edges on the shortest path between them. Algolina’s goal is to make her score exceed Byteasar’s by as much as possible, i.e. to maximize SASBS_A - S_B, while Byteasar’s goal is to minimize it.

Byteasar quickly points out that this is a finite perfect-information game, so assuming both players play optimally, the final score difference can be determined. He hopes you can help compute this value. Since it may be very large, you should output it modulo 109+710^9+7.

Also, since forgetting the gift after just one game would be unpleasant, you need to compute the final score difference for many different choices of vertices AA and BB.

Input Format

The first line contains two integers n,qn, q, representing the number of levels of the tree and the number of queries.

The second line contains n1n-1 integers a1,a2,,an1a_1, a_2, \cdots, a_{n-1}, as described above.

The next qq lines each describe A,BA, B. You may notice that the final result depends only on vertices A,BA, B and on which level their lowest common ancestor lies, so each line provides three integers WA,WB,Wlca(A,B)W_A, W_B, W_{\operatorname{lca}(A,B)}.

Output Format

Output qq lines. The ii-th line should contain the answer to the ii-th query, modulo 109+710^9+7.

3 3
3 2
3 2 1
1 1 1
2 3 2
4
1
1000000003

Hint

Sample 1 Explanation

In the sample, the tree has three levels: one vertex on the first level, three vertices on the second level, and six vertices on the third level.

For the second query, both Algolina and Byteasar choose the root. Under optimal play, they should choose vertices in non-increasing order of level. In the end, the result is (2+2+2+1+1)(2+2+2+1+0)=1(2 + 2 + 2 + 1 + 1) - (2 + 2 + 2 + 1 + 0) = 1.

For the third query, the answer is 4-4, but you should output 4mod(109+7)=109+3-4 \bmod (10^9+7) = 10^9+3.


Constraints

This problem uses bundled testdata.

  • For some subtasks, the tree has at most 3×1053\times 10^5 vertices, and q100q\le 100.
  • For some other subtasks, q100q\le 100.

For each of the above cases, there is at least one such subtask.

For 100%100\% of the testdata, it is guaranteed that 2n3×1052\le n\le 3\times 10^5, 1q3×1051\le q\le 3\times 10^5, 2ai3×1052\le a_i\le 3\times 10^5, and 1Wlca(A,B)WA,WBn1\le W_{\operatorname{lca}(A,B)}\le W_A, W_B\le n.

Translated by ChatGPT 5