Under construction

Still being built. Things may change

Sugar Sweet II

Time limit2sMemory limit2048 MB

Summary
Each child gains w_i when event i fires with fewer sugar than child b_i, events in random order; output each child's expected final sugar mod 1e9+7.
Level

Hard9 of 10

Topics
Probability, Dynamic programming, Divide and conquer
Solved
No attempts yet

Statement

Sugar is sweet.

There are nn children asking for sugar. Prof. Chen gives sugar to the children. The ii-th child initially has aia_i bags of sugar.

There are nn events, happening in uniformly random order. The ii-th event is:

  • If the ii-th child has strictly fewer bags of sugar than the bib_i-th child, the ii-th child gets wiw_i extra bags of sugar. Otherwise, nothing happens.

Because the events happen in random order, Randias, Prof. Chen's assistant, wants the expected number of bags each child has after all events happen.

It can be shown that the answer is an irreducible fraction xy\frac{x}{y}, where xx and yy are integers and y≢0(mod109+7)y \not\equiv 0 \pmod{10^9+7}. Output the integer x⋅y−1 mod (109+7)x \cdot y^{-1} \bmod (10^9+7). That is, output the integer aa with 0≤a<109+70 \le a < 10^9+7 and a⋅y≡x(mod109+7)a \cdot y \equiv x \pmod{10^9+7}.

Input

The first line contains tt (1≤t≤5⋅1051 \le t \le 5 \cdot 10^5), the number of test cases. Each test case is given as follows.

The first line contains nn (1≤n≤5⋅1051 \le n \le 5 \cdot 10^5), the number of children. The second line contains nn integers aia_i (1≤ai≤1091 \le a_i \le 10^9). The third line contains nn integers bib_i (1≤bi≤n1 \le b_i \le n). The fourth line contains nn integers wiw_i (1≤wi≤1091 \le w_i \le 10^9).

The sum of nn over all test cases does not exceed 5⋅1055 \cdot 10^5.

Output

For each test case, print nn integers on one line: the expected number of bags each child has at the end, modulo 109+710^9+7 as described above.

Examples1

  1. Example 1

    Input
    4
    4
    2 5 5 2
    4 2 1 3
    3 2 1 4
    3
    5 4 3
    1 1 1
    6 6 6
    3
    5 4 3
    2 3 1
    1 2 3
    5
    2 1 3 2 1
    5 1 1 3 4
    1 3 4 2 4
    
    Expected output
    500000007 5 5 6
    5 10 9
    166666673 5 6
    500000006 4 3 4 5