Under construction

Still being built. Things may change

Heraclosures

Time limit5.5sMemory limit2048 MB

Summary
Functions form a call DAG with base times and point updates; answer each query for total time T(i) and output the query-index-weighted sum mod 1e9+7.
Level

Hard8 of 10

Topics
Dynamic programming, Graph, Topological sort
Solved
No attempts yet

Problem

Prometheus is a programmer. He wrote a large program in Heraclosures, a programming language invented by his friend Heracles. The program has NN functions, each identified by a unique integer from 11 to NN. Each function consists of many instructions, some of which are call instructions that invoke other functions.

When a call instruction in function ii calls function jj, the full code of function jj runs before function ii continues. A function may contain several call instructions, and it may call the same function several times. Some functions have no call instructions at all. Following advice from his friend Sisyphus, Prometheus made sure the function calls form no cycles, so no call loop can run forever.

Function ii has a base execution time BiB_i, which is its execution time excluding the functions it calls. Execution times are measured in processor cycles, so BiB_i is always a non-negative integer. The total execution time of function ii, written T(i)T(i), is its base time plus the total time of every function it calls directly:

T(i)=Bi+∑j∈C(i)T(j)T(i) = B_i + \sum_{j \in C(i)} T(j)

Here C(i)C(i) is the multiset of functions that ii calls directly. If ii calls jj several times, T(j)T(j) appears that many times in the sum. If ii has no call instructions, C(i)C(i) is empty and T(i)=BiT(i) = B_i.

An update sets BiB_i to a value VV. The update changes only the base time, and the multiset C(i)C(i) stays the same. A query asks for the current total execution time T(i)T(i) of function ii, using all updates that appear before it in the event list.

Prometheus wants one summary value instead of each query result. With qq queries numbered 1 to qq in event order, the summary is

(∑k=1qk⋅T(ik)) mod (109+7)\left( \sum_{k=1}^{q} k \cdot T(i_k) \right) \bmod (10^9+7)

where T(ik)T(i_k) is the total execution time of the function in the kk-th query, using all updates before that query.

Input

The first line contains an integer NN (1≤N≤80001 \le N \le 8000), the number of functions.

The next line contains NN integers B1,B2,…,BNB_1, B_2, \dots, B_N (0≤Bi≤1090 \le B_i \le 10^9), the base execution time of each function.

The next line contains an integer MM (1≤M≤80001 \le M \le 8000), the number of call instructions among all functions. Each of the next MM lines contains two integers FF and GG (1≤F,G≤N1 \le F, G \le N, F≠GF \ne G), meaning function FF contains a call instruction to function GG.

The next line contains an integer EE (1≤E≤1061 \le E \le 10^6), the number of events. Each of the next EE lines is either an update, written as the uppercase letter U followed by two integers II (1≤I≤N1 \le I \le N) and VV (0≤V≤1090 \le V \le 10^9), which sets BIB_I to VV, or a query, written as the uppercase letter Q followed by an integer JJ (1≤J≤N1 \le J \le N), which asks for T(J)T(J).

The input has no call cycles, and there is at least one query.

Output

Print a single integer, the summary result of all queries modulo 109+710^9+7.

Examples2

  1. Example 1

    Input
    3
    10 20 100
    3
    1 2
    2 3
    1 3
    3
    Q 1
    Q 2
    Q 3
    
    Expected output
    770
    
  2. Example 2

    Input
    2
    42 10
    2
    2 1
    2 1
    5
    Q 2
    Q 1
    U 2 0
    Q 1
    Q 2
    
    Expected output
    640