#P9711. [KMOI R1] 五五五五(Hard)

[KMOI R1] 五五五五(Hard)

Background

“Things can be inferred from each other, and each has its place. So although branches split, they share the same trunk. This only reveals one end of it. Also, when analyzing principles through words and explaining structures with diagrams, it may be concise yet complete, clear yet not verbose. Readers will understand more than half just by thinking.” — Liu Hui.

Problem Description

Xiao Song has a sequence A={a1,a2,an}A=\{a_1,a_2\dots,a_n\}, where i[1,n],ai[0,9]\forall i\in [1,n],a_i\in[0,9].

For 1lrn1\le l\le r\le n, he defines f(l,r)f(l,r) as the number of consecutive trailing 55’s at the end of ala(l+1)ar\overline{a_la_{(l+1)}\dots a_r}.

For example, for the sequence a={1,1,4,5,1,4}a=\{1,1,4,5,1,4\}, f(2,4)=1,f(1,3)=0f(2,4)=1,f(1,3)=0.

Xiao Song will keep performing operations on this sequence. Specifically, he will do the following operations:

  • (1,x,y)(1,x,y): Change the xx-th number to yy (x[1,n],y[0,9]x\in[1,n],y\in[0,9]).

  • 22: Reverse the sequence aa. For example, {1,1,4,5}\{1,1,4,5\} becomes {5,4,1,1}\{5,4,1,1\} after reversing.

  • 33: Query the sequence.

  • (4,l,r)(4,l,r): Query the sequence.

For each operation 33, output:

$$\Big(\sum\limits_{l=1}^ {n}\sum\limits_{r=l}^{n} f(l,r)\Big) \bmod 10^9+7$$

For each operation 44, output:

(i=lrai)mod109+7\Big(\sum\limits_{i=l}^{r}a_i\Big) \bmod 10^9+7

Input Format

The first line contains two positive integers n,qn,q, representing the length of the sequence and the number of queries.

The second line contains nn integers a1,a2,ana_1, a_2,\dots a_n, representing the sequence AA.

The next qq lines each contain one or three positive integers, representing an operation.

Output Format

For each operation 33 and operation 44, output the answer.

3 4
1 5 5
1 3 3
3
1 1 5
4 1 3
2
13
6 5
1 1 4 5 1 4
3
2
3
1 1 5
4 1 4
4
3
15

Hint

Explanation for Sample 11:

Operation Sequence after the operation Answer
(1,3,3)(1,3,3) {1,5,3}\{1,5,3\} //
33 // 22
(1,1,5)(1,1,5) {5,5,3}\{5,5,3\} //
(4,1,3)(4,1,3) // 1313

Explanation for Sample 22:

Operation Sequence after the operation Answer
33 // 44
22 {4,1,5,4,1,1}\{4,1,5,4,1,1\} //
33 // 33
(1,1,5)(1,1,5) {5,1,5,4,1,1}\{5,1,5,4,1,1\} //
(4,1,4)(4,1,4) // 1515

Constraints

Test Point ID nn\le qq\le Special Property
11 100100 //
2,32,3 10310^3 A\mathbf{A}
44 B\mathbf{B}
5105\sim10 2×1052\times 10^5 //
111311\sim13 A\mathbf{A}
14,1514,15 //
161816\sim18 5×1055\times 10^5 B\mathbf{B}
192519\sim25 //

Special property A\mathbf{A}: There is no operation 22.

Special property B\mathbf{B}: There is no operation 33.

For 100%100\% of the testdata: 1n5×1051\le n\le 5\times 10^5, 1q5×1051\le q\le 5\times 10^5.

i[1,n]\forall i\in [1,n], ai[0,9]a_i\in[0,9].

Translated by ChatGPT 5