Under construction

Still being built. Things may change

Logistics

Time limit15sMemory limit1024 MB

Summary
For each store, count the valid production sequences from factory 1 that end at the store's factory with a level in its range, modulo 1e9+7.
Level

Hard8 of 10

Topics
Tree, Dynamic programming, DFS
Solved
No attempts yet

Statement

Universal Manufacturing, a world-famous industrial company, has recently decided to expand its business. The company makes many kinds of products, from lamps to tractors, and handles the entire production process from raw materials to finished products inside its own factories.

The company has NN factories numbered 1…N1 \ldots N, where factory 1 is the main factory. The factories are connected by N−1N-1 roads, and it is known that every factory can be reached from the main factory by following these roads.

Factory ii has a level TiT_i. The factory can produce components of level TiT_i for other factories, as well as finished products of the same level. A factory may use components of any lower level as input, but the component or product it produces always has level TiT_i.

The company has opened MM stores. Store ii is located at factory CiC_i and can sell products with levels from LiL_i to RiR_i. Each product passes through a sequence of factories (j1,j2,…,jc)(j_1, j_2, \ldots, j_c). Basic components are made at factory j1j_1 and sent to factory j2j_2, where higher-level components are made from them. This continues until factory jcj_c, where the final product is made and transported to the store. Basic components are always made at the main factory, so j1=1j_1 = 1. Since each factory can only process components of a lower level, Tji<Tji+1T_{j_i} < T_{j_{i+1}} must hold for every 1≤i<c1 \le i < c.

Transport is the most expensive part of production. During the production of any single product, no factory may be visited more than once, either by a component or by the finished product, even if the product is not processed there. If no store can sell a product, that product is not sold at any store.

Input

The first line contains the number of factories NN (1≤N≤1051 \le N \le 10^5). The second line contains NN integers P1,…,PNP_1, \ldots, P_N (1≤Pi<i1 \le P_i < i), where PiP_i means that there is a road between factory ii and factory PiP_i. The exception is P1=0P_1 = 0, which means there is no road. The third line contains NN integers T1,…,TNT_1, \ldots, T_N (0≤Ti<N0 \le T_i < N), the levels of the factories. It is known that T1=0T_1 = 0 and that all TiT_i values are distinct. The fourth line contains the number of stores MM (1≤M≤1051 \le M \le 10^5). Each of the last MM lines contains three integers CiC_i, LiL_i, and RiR_i (1≤Ci≤N1 \le C_i \le N, 0≤Li≤Ri<N0 \le L_i \le R_i < N), meaning that store ii is located at factory CiC_i and can sell products with levels from LiL_i to RiR_i.

Output

For each store ii, print on its own line the total number of factory sequences that correspond to all possible production processes for the products that store ii can sell. Since this number can be very large, print the remainder when it is divided by 109+710^9+7 instead of the exact value.

Examples1

  1. Example 1

    Input
    8
    0 1 2 2 4 2 1 7
    0 7 5 1 3 6 4 2
    4
    5 1 4
    8 2 4
    6 1 4
    2 1 7
    
    Expected output
    3
    2
    0
    1