#P5142. 区间方差

区间方差

Description

For a sequence of length nn, a1,a2,a3ana_1,a_2,a_3\cdots a_n, we define its mean aa as:

a=1ni=1naia=\frac{1}{n}\sum_{i=1}^{n}a_i

and define its variance dd as:

d=1ni=1n(aia)2d=\frac{1}{n}\sum_{i=1}^{n}(a_i-a)^2

Now you are given a sequence b1,b2bnb_1,b_2\cdots b_n of length nn. You need to support two operations. Each operation has the format c x y.

If c=1c=1, it is an update: set bxb_x to yy.

If c=2c=2, it is a query: compute the variance of bxb_x through byb_y (inclusive).

To avoid floating-point errors, output the result as a fraction modulo (mod 10000000071000000007 (109+710^9+7)).

Input Format

The first line contains two integers n,mn,m, meaning the sequence bb has length nn and there are mm operations.

The second line contains nn integers bib_i, representing the initial values of sequence bb.

Then there are mm lines, each in the format c x y, with meanings as described above. All operations are guaranteed to be valid.

Output Format

For each operation 22, output one line.

4 8
0 0 0 0
1 1 1
1 2 2
1 3 3
1 4 4
2 1 1
2 1 2
2 1 3
2 1 4
0
250000002
666666672
250000003

Hint

Explanation for Sample 1

After four updates, the sequence bb is: {1,2,3,4}\{1,2,3,4\}.

The variance of interval [1,1][1,1] is 00.

The variance of interval [1,2][1,2] is 14\frac{1}{4}. The modular inverse of 44 is 250000002250000002.

The variance of interval [1,3][1,3] is 23\frac{2}{3}. The modular inverse of 33 is 333333336333333336, and 2×333333336modM=6666666722\times333333336\bmod M=666666672.

Constraints

  • For 50%50\% of the testdata, n1000n\leq 1000, m1000m\leq 1000.
  • For 100%100\% of the testdata, 1n,m1×1051\leq n,m\leq 1\times 10^5, 1bi1×1091\leq b_i\leq 1\times 10^9, 1xn1\leq x\leq n. For operation 11, 1y1×1091\leq y\leq 1\times 10^9. For operation 22, xynx\leq y\leq n.

Translated by ChatGPT 5