Sequence and queries 12

Maintain a dynamic sequence under point updates, deletions, and insertions, answering range queries for distinct count and the sum of triple products of distinct values.

Hard9Segment treeHash mapCombinatoricsImplementationNo attempts yetTime limit2sMemory limit512 MB

Problem

You are given a sequence A1,A2,,ANA_1, A_2, \dots, A_N of length NN. Write a program that processes the following five kinds of queries in the order they are given.

  • 1 l r: let SS be the set of distinct values that appear from the ll-th number to the rr-th number, sorted in increasing order, and print the value shown below.
  • 2 x y: change AxA_x to yy.
  • 3 x: delete the xx-th number.
  • 4 z y: insert yy right after the zz-th number. If z=0z = 0, insert yy at the front of the sequence.
  • 5 l r: print how many distinct numbers appear from the ll-th number to the rr-th number.

The answer to a query of type 1 is

(1i<j<kSSiSjSk)mod(109+7)\left(\sum_{1 \le i < j < k \le |S|} S_i S_j S_k\right) \bmod (10^9+7)

Indices of the sequence start at 1. A value that appears several times inside the range belongs to SS only once, and the answer to a query of type 1 is 0 when S|S| is smaller than 3. The size of the sequence is always at least 1.

Input

The first line contains the size of the sequence, NN.

The second line contains A1,A2,,ANA_1, A_2, \dots, A_N.

The third line contains the number of queries, MM.

Each of the next MM lines contains one query.

  • 1N,M100,0001 \le N, M \le 100{,}000
  • 1Ai,y109+61 \le A_i, y \le 10^9+6
  • 1xA1 \le x \le |A|
  • 1lrA1 \le l \le r \le |A|
  • 0zA0 \le z \le |A|

Here A|A| is the size of the sequence right before that query is processed.

Output

For every query of type 1 and every query of type 5, print the answer on its own line, in the order the queries are given.