#P11891. [XRCOI Round 1] B. 稻花香里说丰年

[XRCOI Round 1] B. 稻花香里说丰年

Background

Add a more formal problem statement.

Talking about a good harvest amid the fragrance of rice flowers, listening to a chorus of frogs.

Problem Description

In a rice field there are frogs of two types: 00 and 11. They line up into two columns, aa and bb.

Genius_Star noticed these frogs, so he decided to split the two frog sequences into arbitrary segments with the same start and end positions. Define the neatness of a segment as:

T(l,r)=i=lr[aibi]T(l,r) = \sum_{i=l}^r [a_i \ne b_i]

Here, [A][A] equals 11 if proposition AA is true, otherwise 00.

Then the happiness value brought to Genius_Star by segment [l,r][l,r] is:

W(l,r)=T(l,r)×(A(l,r)+B(l,r))W(l,r) = T(l,r) \times \Big(A(l,r) + B(l,r) \Big)

where:

A(l,r)A(l,r) is the number of subsequences of type 0101 contained in interval [l,r][l,r] of sequence aa.

B(l,r)B(l,r) is the number of subsequences of type 1010 contained in interval [l,r][l,r] of sequence bb.

The happiness value of a segmentation plan is the sum of the happiness values of all its segments. You need to compute the sum ss of the happiness values over all segmentation plans.

Suppose the sequences are split into kk segments, and the ii-th segment is [li,ri](liri)[l_i, r_i](l_i \le r_i). The following must hold:

  • l1=1,rk=nl_1 = 1, r_k = n.
  • i[1,k),ri+1=li+1\forall i \in[1, k), r_i + 1 = l_{i + 1}.

Two segmentation plans are different if and only if they have different numbers of segments, or there exists some ii such that [li,ri][li,ri][l_i, r_i] \ne [l'_i, r'_i].

Since Genius_Star cannot be too happy, you also need to take the answer modulo 109+710^9+7.

Formal statement

Given two sequences a,ba,b, define:

  • T(l,r)T(l,r) as the number of positions in interval [l,r][l,r] where aibia_i\neq b_i.
  • A(l,r)A(l,r) as the number of subsequences of type 0101 in interval [l,r][l,r] of sequence aa.
  • B(l,r)B(l,r) as the number of subsequences of type 1010 in interval [l,r][l,r] of sequence bb.
  • $W(l,r) = T(l,r) \times \Big(A(l,r) + B(l,r) \Big)$.

Here, a subsequence of type xyxy means selecting two elements from left to right in the interval (not necessarily contiguous), where the first number is xx and the second number is yy. Two subsequences are different if and only if the position of xx or the position of yy is different.

Now you need to split the two sequences into arbitrary segments with the same start and end positions, and compute the sum, over all splitting methods, of the total WW of all segments.

Since the answer may be very large, output it modulo 109+710^9+7.

Input Format

The first line contains two positive integers n,opn,op.

When op=0op=0, the values ai,bia_i,b_i for this test are given directly:

  • The next nn lines each contain two integers ai,bia_i,b_i.

When op=1op=1, the values ai,bia_i,b_i for this test will be generated in a special way:

  • The second line contains five integers x1,y1,z1,f1,f2x_1, y_1, z_1, f_1, f_2.
  • The third line contains five integers x2,y2,z2,g1,g2x_2, y_2, z_2, g_1, g_2.

Then fi,gi(i3)f_i,g_i(i \ge 3) satisfy:

$$f_i = (f_{i-2} \times x_1 + f_{i-1} \times y_1 + z_1) \bmod 2^{64}$$$$g_i = (g_{i-2} \times x_2 + g_{i-1} \times y_2 + z_2) \bmod 2^{64}$$

Then:

  • The value of aia_i is the ((i1mod64)+1)((i-1 \bmod 64)+1)-th bit from low to high in the binary representation of fi164+3f_{\lfloor \frac{i-1}{64} \rfloor+3}.
  • The value of bib_i is the ((i1mod64)+1)((i-1 \bmod 64)+1)-th bit from low to high in the binary representation of gi164+3g_{\lfloor \frac{i-1}{64} \rfloor+3}.

The above generation method is only to reduce input size; the standard algorithm does not depend on it.

Output Format

Output one line with an integer ss, meaning the sum of the happiness values of all segmentation plans.

3 0
1 0
1 1
0 0

1

5 0
0 1
1 0
0 1
1 1
1 1

70

4 0
0 1
1 0
0 1
1 0

52

5 1
1 1 0 1 1
1 1 1 1 1

22

10000 1
1 1 0 1 1
1 1 1 1 1

559297012

Hint

Sample explanation #1:

Only when splitting as [1,3][1,3], we have W(1,3)=1W(1,3)=1.

Sample explanation #4:

We can obtain:

f1=1,f2=1,f3=2f_1=1,f_2=1,f_3=2 g1=1,g2=1,g3=3g_1=1,g_2=1,g_3=3

Thus:

(f3)2=(00010)2(f_3)_2 = (00010)_2 (g3)2=(00011)2(g_3)_2 = (00011)_2

Then:

a={0,1,0,0,0}a = \{0,1,0,0,0\} b={1,1,0,0,0}b = \{1,1,0,0,0\}

So the answer is 2222.

Constraints

This problem uses bundled tests.

Subtask 00 is the sample and is worth 00 points.

Subtask ID nn opop Special Property Points
11 100\leq 100 =0=0 None 55
22 104\leq 10^4 1010
33 106\leq 10^6 AA
44 BB
55 =1=1 None 2525
66 5×107\leq 5\times 10^7 4040

Special property AA: T(l,r)=rl+1T(l,r) = r-l+1.

Special property BB: there exists exactly one xx such that axbxa_x \ne b_x.

For 100%100\% of the testdata, it is guaranteed that $1 \le n \le 5\times 10^7,1 \le x_1,x_2,y_1,y_2,z_1,z_2,f_1,f_2,g_1,g_2 \le 10^{18}$.

Translated by ChatGPT 5