Buses from Bragadiru

Time limit: 0.5s Memory limit: 128MB Input: Output:

Bragadiru, a city on the outskirts of Bucharest, has just revamped its bus network! Before leaving for Piatra Neamț, Andrei has to take the bus from his station to another stop, where he will meet another Andrei (from the legendary team Andrei of 2024). He observed that the city hall has imposed a new pricing model for the buses:

  • Each stop ii has two associated costs: departidepart_i, the price to leave from station ii, and arriveiarrive_i, the price to arrive at station ii.
  • Traveling directly from station ii to station jj costs C(i,j)=departiarrivejC(i, j) = depart_i \cdot arrive_j lei.
  • A bus trip is a sequence of stops X0,X1,,XkX_0, X_1, \ldots, X_k where each consecutive pair is connected by a direct hop (stops may repeat). Its length is kk -- the number of hops -- and its total cost is the product C(X0,X1)C(X1,X2)C(Xk1,Xk)C(X_0, X_1) \cdot C(X_1, X_2) \cdots C(X_{k-1}, X_k) lei.

Task

Andrei watches a stream of QQ events. Some are pricing adjustments published by the city hall (the prices change in real time); others are questions Andrei asks about the network in its current state. Each event has one of the following forms:

  • 1 L R val\text{1 L R val} -- (pricing update) departideparti+valdepart_i \gets depart_i + val for all LiRL \leq i \leq R.
  • 2 L R val\text{2 L R val} --(pricing update) arriveiarrivei+valarrive_i \gets arrive_i + val for all LiRL \leq i \leq R.
  • 3 L R k\text{3 L R k} -- (query) Restrict the network to only the stops in the interval [L,R][L, R]. Compute the sum of total costs of all trips of length exactly kk whose stops lie entirely within [L,R][L, R].

Since the answer to a query can be very large, output it modulo 109+710^9 + 7.

Input data

The first line of the input contains an integer NN, representing the number of bus stops in Bragadiru.

The second line contains NN space-separated integers, depart1,depart2,,departNdepart_1, depart_2, \ldots, depart_N, representing the cost to depart from each stop.

The third line contains NN space-separated integers, arrive1,arrive2,,arriveNarrive_1, arrive_2, \ldots, arrive_N, representing the cost to arrive at each stop.

The fourth line contains an integer QQ, representing the number of events.

The following QQ lines describe the events. Each line begins with an integer type{1,2,3}type \in \{1, 2, 3\}, followed by additional space-separated integers corresponding to the parameters described above.

Output data

For each query event (type 33), output on a separate line the requested sum, taken modulo 109+710^9 + 7.

Constraints and clarifications

  • 1N,Q100 0001 \leq N, Q \leq 100 \ 000
  • 1arrivei,departi10 0001 \leq arrive_i, depart_i \leq 10 \ 000 for all 1iN1 \leq i \leq N.
  • 1LRN1 \leq L \leq R \leq N for every event.
  • 1val10 0001 \leq val \leq 10 \ 000 for every event of type 1 or 2.
  • 1k1091 \leq k \leq 10^9 for every event of type 3.
# Score Constraints
0 0 Examples
1 10 N,Q1000N, Q \leq 1000 and k20k \leq 20 for every query.
2 13 N,Q100N, Q \leq 100.
3 15 N,Q5000N, Q \leq 5000 and k100k \leq 100 for every query.
4 7 k=1k = 1 for every query.
5 15 No update events occur -- only queries.
6 17 Only one of the two update types (type 1 or type 2) appears in the input.
7 23 No further restrictions.

Example

stdin

3
2 3 1
1 2 4
4
3 1 3 1
1 2 2 5
3 1 3 1
3 2 3 2

stdout

42
77
1080

Explanation

Initially depart=[2,3,1]depart = [2, 3, 1] and arrive=[1,2,4]arrive = [1, 2, 4].

  • 3 1 3 1\text{3 1 3 1}: query on [1,3][1, 3] with k=1k = 1. Each trip has a single hop iji \to j for i,j{1,2,3}i, j \in \{1, 2, 3\}, with cost departiarrivejdepart_i \cdot arrive_j. Summing over all 99 pairs gives 4242.
  • 1 2 2 5\text{1 2 2 5}: update -- depart2depart_2 becomes 3+5=83 + 5 = 8, so depart=[2,8,1]depart = [2, 8, 1].
  • 3 1 3 1\text{3 1 3 1}: same kind of query, now (2+8+1)(1+2+4)=117=77(2 + 8 + 1) \cdot (1 + 2 + 4) = 11 \cdot 7 = 77.
  • 3 2 3 2\text{3 2 3 2}: query on [2,3][2, 3] with k=2k = 2. A trip of length 22 visits a sequence of three stops X0X1X2X_0 \to X_1 \to X_2, each in {2,3}\{2, 3\}, and

stops may repeat -- e.g. 2222 \to 2 \to 2 or 3323 \to 3 \to 2 are perfectly valid trips. So there are 23=82^3 = 8 trips in total, not just the ones that use distinct stops.

With depart=[2,8,1]depart = [2, 8, 1] and arrive=[1,2,4]arrive = [1, 2, 4], the four edge costs within {2,3}\{2, 3\} are

C(2,2)=82=16,C(2,3)=84=32,C(3,2)=12=2,C(3,3)=14=4.C(2,2) = 8 \cdot 2 = 16, \quad C(2,3) = 8 \cdot 4 = 32, \quad C(3,2) = 1 \cdot 2 = 2, \quad C(3,3) = 1 \cdot 4 = 4.


Each trip's cost is the product of its two edges:

222:1616=256223:1632=512232:322=64233:324=128322:216=32323:232=64332:42=8333:44=16\begin{array}{llll} 2\to2\to2: 16\cdot16=256 & 2\to2\to3: 16\cdot32=512 & 2\to3\to2: 32\cdot2=64 & 2\to3\to3: 32\cdot4=128 \\ 3\to2\to2: 2\cdot16=32 & 3\to2\to3: 2\cdot32=64 & 3\to3\to2: 4\cdot2=8 & 3\to3\to3: 4\cdot4=16 \end{array}


Their sum is 256+512+64+128+32+64+8+16=1080256 + 512 + 64 + 128 + 32 + 64 + 8 + 16 = 1080.

Log in or sign up to be able to send submissions!