Under construction

Still being built. Things may change

Gasoline Prices

Time limit3.5sMemory limit1024 MB

Summary
Given a tree with a price range per station, count valid price assignments after each year's pair of path constraints forces matching prices along both paths.
Level

Hard9 of 10

Topics
Tree, Union-find, String matching
Solved
No attempts yet

Problem

Berlandia is a large country with nn cities. Its road network is a rooted tree, so the country has n−1n - 1 roads, and between any two cities there is exactly one path that visits no city twice. For each city ii, pip_i is the city that comes first on the way from city ii to city 1. In other words, when the tree is hung from city 1, pip_i is the parent of city ii. Each city has one gas station. Station ii can sell gasoline at any price from lil_i to rir_i inclusive. The king has two sons every year for mm years. The sons take part in state affairs from early childhood, and at the end of each year they check that gasoline prices are fair. The sons born in year ii check prices on the path from aia_i to bib_i and on the path from cic_i to did_i. The check works as follows. Both sons start at the same time, from aia_i and cic_i. The first son walks from aia_i to bib_i, and the second walks from cic_i to did_i. They check that the price at aia_i equals the price at cic_i. Then they check that the prices at the second cities of the two paths are equal. They repeat this for the third cities, and so on. Finally they check that the price at bib_i equals the price at did_i. The two paths are guaranteed to have the same length. The stations must obey the law, so no check may find a violation. Count the ways the stations can set prices over mm years. For each ii from 1 to mm, count the ways to set prices at all stations so that after the first ii pairs of sons are born, every check passes and every station's price is within its allowed range. Print the answer modulo 109+710^9+7.

Input

The first line contains nn (1≤n≤2000001 \le n \le 200000), the number of cities. The second line contains n−1n-1 integers p2,p3,…,pnp_2, p_3, \ldots, p_n (1≤pi≤n1 \le p_i \le n). Each of the next nn lines contains lil_i and rir_i (1≤li≤ri<109+71 \le l_i \le r_i < 10^9+7), the allowed price range at station ii. The next line contains mm (1≤m≤2000001 \le m \le 200000). Each of the next mm lines contains aia_i, bib_i, cic_i, and did_i (1≤ai,bi,ci,di≤n1 \le a_i, b_i, c_i, d_i \le n). The lengths of the paths from aia_i to bib_i and from cic_i to did_i are guaranteed to be equal.

Output

Print mm lines, each with one integer. The ii-th line is the number of ways to set prices so that the sons born in years up to and including year ii find no violation, modulo 109+710^9+7.

Hint

Consider the first example. After the first two sons are born, the prices at cities 1 and 2 must be equal. There are 2 ways to choose one shared price for cities 1 and 2 that lies in both allowed ranges. So the total is 2⋅3⋅3⋅1=182 \cdot 3 \cdot 3 \cdot 1 = 18. The second pair checks the paths 1−21 - 2 and 2−12 - 1. The prices at cities 1 and 2 are already equal, so the answer does not change after the second pair. The third pair checks the paths 3−1−2−43 - 1 - 2 - 4 and 4−2−1−34 - 2 - 1 - 3. Then the prices at cities 3 and 4 must be equal, and the prices at cities 1 and 2 must be equal. Cities 1 and 2 already match. There are 2 ways to choose a shared price for cities 3 and 4 within their ranges. So the total is 2⋅2⋅1=62 \cdot 2 \cdot 1 = 6. The fourth pair checks the paths 3−1−2−43 - 1 - 2 - 4 and 3−1−2−53 - 1 - 2 - 5. Then the prices at cities 4 and 5 must be equal. Since cities 3 and 4 already match, cities 3, 4, and 5 must share one price. The price at city 3 must be at most 3, and the price at city 5 must be at least 4. So after the fourth pair is born, there is no way to set prices that pass every check and keep each price in its range.

Examples2

  1. Example 1

    Input
    5
    1 1 2 2
    2 4
    1 3
    1 3
    2 4
    4 4
    4
    1 1 2 2
    1 2 2 1
    3 4 4 3
    3 4 3 5
    
    Expected output
    18
    18
    4
    0
    
  2. Example 2

    Input
    8
    1 2 3 4 5 8 6
    3 7
    2 6
    3 8
    5 10
    5 8
    2 9
    3 8
    6 8
    4
    1 3 7 6
    4 1 5 7
    1 7 7 1
    1 8 2 7
    
    Expected output
    720
    120
    120
    1