#P7770. 「CGOI-1」丑国旅游

「CGOI-1」丑国旅游

Description

In one part of Ugly Country, there are nn cities numbered from 11 to nn. When a person is in city ii, they can and can only go to city i+1i+1.

People in city ii hate people whose ugliness value is aia_i the most. When a person with ugliness value xx walks from city ii to city i+1i+1, they gain a comfort value of xai×xai+1|x-a_i|\times|x-a_{i+1}|.

Now there are mm people coming to travel in Ugly Country. The ii-th person has ugliness value xix_i and will walk from city lil_i to city rir_i. Ask for the sum of the comfort values they obtain.

Since this number may be very large, you need to output it modulo 109+710^9+7.

Since you cannot predict the next trip, the queries will be forced to be online.

Simplified statement:

Given nn and nn integers a1,a2,,ana_1,\,a_2,\,\dots,\,a_n.

There are mm online queries. Each query gives x,l,rx,\,l,\,r. Compute i=lr1xai×xai+1\sum\limits_{i=l}^{r-1}|x-a_i|\times|x-a_{i+1}|.

Input Format

The first line contains two integers n,mn,m, representing the number of cities and the number of tourists.

The second line contains nn integers. The ii-th number is aia_i, with the meaning as described above.

In the next mm lines, each line contains three integers X,L,RX,\,L,\,R. Let ss be the total comfort value of the previous query modulo 109+710^9+7 (if this is the first query, then s=0s=0). Then $x_i=X\operatorname{xor}s,\;l_i=L\operatorname{xor}s,\;r_i=R\operatorname{xor}s$, where xor\operatorname{xor} denotes bitwise XOR, and the meanings of xi,li,rix_i,\,l_i,\,r_i are as described above.

Output Format

Output mm lines. The ii-th line is the ii-th person's total comfort value modulo 109+710^9+7.

5 2
1 2 3 4 5
1 1 3
6 1 7
2
0

Hint

Sample Explanation:

For the first query, walking from city 11 to city 22 gives a comfort value of 11×12=0|1-1|\times|1-2|=0. Walking from city 22 to city 33 gives a comfort value of 12×13=2|1-2|\times|1-3|=2. Therefore, the total comfort value is 22.

For the second query, the decrypted x,l,rx,\,l,\,r are 4,3,54,3,5. Walking from city 33 to city 44 gives a comfort value of 43×44=0|4-3|\times|4-4|=0. Walking from city 44 to city 55 gives a comfort value of 44×45=0|4-4|\times|4-5|=0. The total comfort value is 00.


Constraints:

This problem uses bundled testdata.

ID Special Constraints Score Time Limit
Subtask0 n,m104n,\,m\le 10^4 20pts 1s
Subtask1 ai,x10a_i,\,x\le 10 10pts 2s
Subtask2 aia_i is strictly increasing
Subtask3 No special constraints 60pts

For 100%100\% of the testdata, 1n,m3×1051 \le n,\,m \le 3 \times 10^5, 1ai,xi1091 \le a_i,\,x_i \le 10^9, 1li<rin1 \le l_i < r_i \le n.

Translated by ChatGPT 5