Under construction

Still being built. Things may change

Tree of Charge

Time limit4sMemory limit512 MB

Summary
Charges on a rooted tree move up to parents, split down equally among children, or get added at a vertex. Print each final charge modulo 1e9+7.
Level

Hard8 of 10

Topics
Tree, Math, Simulation
Solved
No attempts yet

Problem

A rooted tree is given. Each vertex vv holds a non-negative charge cvc_v. You must process three kinds of queries.

  • Move the charge up: all vertices simultaneously move their entire charge to their direct parent. The charge at the root is not moved anywhere. In other words, if vertex vv has children u1,…,uku_1, \ldots, u_k, then its new charge is cu1+⋯+cukc_{u_1} + \cdots + c_{u_k} when vv is not the root, and cu1+⋯+cuk+cvc_{u_1} + \cdots + c_{u_k} + c_v when vv is the root.
  • Move the charge down: all vertices simultaneously split their charge equally among their children. If vertex vv has children u1,…,uku_1, \ldots, u_k, then the new charge of each uiu_i is cv/kc_v / k. Each leaf has a virtual straight line tree of sufficient length attached to it. So if you move the charge down from a leaf and then move it up the same number of times, the leaf keeps its original charge.
  • Add charge: add xix_i to the charge of vertex viv_i.

After processing all queries, print the charge of every vertex.

Input

The first line contains the number of vertices nn (2≤n≤500 0002 \le n \le 500\,000). The second line contains the initial charges cic_i (0≤ci<109+70 \le c_i < 10^9 + 7). Each of the next n−1n-1 lines contains two integers uiu_i and viv_i, an edge of the tree (1≤ui,vi≤n1 \le u_i, v_i \le n). The input graph is guaranteed to be a tree.

The next line contains the number of queries qq (0≤q≤500 0000 \le q \le 500\,000). Each of the next qq lines is one query. A move up query is written as ^. A move down query is written as v. An add charge query is written as + followed by two integers viv_i and xix_i, meaning add charge xix_i to vertex viv_i (1≤vi≤n1 \le v_i \le n, 0≤xi<109+70 \le x_i < 10^9 + 7).

Output

Print nn numbers. The ii-th number is the final charge of vertex ii.

Let the charge be the fraction p/qp/q. Print the unique integer xx with 0≤x<109+70 \le x < 10^9 + 7 such that p≡x⋅q(mod109+7)p \equiv x \cdot q \pmod{10^9 + 7}.

Examples3

  1. Example 1

    Input
    5
    4 3 3 6 0
    1 2
    1 3
    2 4
    4 5
    5
    v
    + 1 7
    ^
    + 2 1
    v
    
    Expected output
    0 500000009 500000009 4 6
    
  2. Example 2

    Input
    2
    5 10
    1 2
    5
    v
    v
    v
    ^
    ^
    
    Expected output
    0 5
    
  3. Example 3

    Input
    4
    0 1 0 0
    1 2
    1 3
    1 4
    2
    ^
    v
    
    Expected output
    0 333333336 333333336 333333336