Weighted sum queries on a mutable sequence

Maintain a sequence under insert, delete, and replace, and answer weighted-sum range queries where each element is multiplied by its offset to the power k (k up to 10).

Hard8TreeBinary searchPrefix sumMathNo attempts yetTime limit2sMemory limit512 MB

Problem

You are given a sequence A0,A1,,AN1A_0, A_1, \dots, A_{N-1} of length NN. Every element satisfies 0Ai<2320 \le A_i < 2^{32}. Process the following four queries in the given order.

  • 1 p v: insert vv immediately before ApA_p. If pp equals the current length of the sequence, append vv at the end. (0p0 \le p \le length of the sequence, 0v<2320 \le v < 2^{32})
  • 2 p: remove ApA_p. (0p<0 \le p < length of the sequence)
  • 3 p v: replace ApA_p with vv. (0p<0 \le p < length of the sequence, 0v<2320 \le v < 2^{32})
  • 4 l r k: print (i=lrAi×(il+1)k)mod232\left( \sum_{i=l}^{r} A_i \times (i - l + 1)^k \right) \bmod 2^{32}. (0lr<0 \le l \le r < length of the sequence, 0k100 \le k \le 10)

An insertion or a removal shifts the index of every element after it by one. The values pp, ll, and rr in a query refer to the sequence as it stands when that query is processed.

The weight (il+1)k(i - l + 1)^k in query 4 is the position of AiA_i inside the range, counted from the left end starting at 1, raised to the power kk.

Input

The first line contains the size of the sequence NN (1N1000001 \le N \le 100000).

The second line contains A0,A1,,AN1A_0, A_1, \dots, A_{N-1}, separated by spaces.

The third line contains the number of queries MM (1M1000001 \le M \le 100000).

Each of the next MM lines contains one query.

Output

For every query of type 4, print the answer on its own line. Queries of type 1, 2, and 3 print nothing.