#P11326. 【MX-S7-T4】「SMOI-R2」XA-Game

【MX-S7-T4】「SMOI-R2」XA-Game

Background

Original problem link: https://oier.team/problems/S7D.

Problem Description

Alice and Bob are playing a game.

Initially, there is a 01 sequence of length nn, with positions numbered 1,2,,n1,2,\dots,n. The two players take turns operating, and Alice moves first.

Alice's move is to choose any two adjacent numbers in the sequence and merge them into their xor value (i.e. the ^ operator in C++).

Bob's move is to choose any two adjacent numbers in the sequence and merge them into their and value (i.e. the & operator in C++).

The game continues until there is only one number left in the sequence. If the final remaining number is 11, Alice wins; otherwise, Bob wins.

Before the game starts, Bob casts mm spells to adjust the initial sequence. The ii-th spell changes the number at position aia_i to viv_i.

Alice wants to know: if both players use optimal strategies, how many possible initial sequences (before Bob casts his spells) can allow her to win the game. Since the answer may be very large, you need to output it modulo the prime 109+710^9 + 7.

Input Format

This problem contains multiple test cases.

The first line contains a positive integer TT, the number of test cases. Then for each test case:

  • The first line contains two non-negative integers n,mn, m, representing the length of the 01 sequence and the number of Bob's spell operations.
  • The next mm lines: the ii-th line contains two non-negative integers ai,via_i, v_i, meaning that the ii-th spell changes the number at position aia_i to viv_i. It is guaranteed that ai\boldsymbol{a_i} are given in strictly increasing order, i.e. 1a1<a2<<amn\boldsymbol{1 \le a_1 < a_2 < \cdots < a_m \le n}.

Output Format

For each test case:

  • Output a single line with one integer, the answer modulo 109+710^9 + 7.
5
3 0
5 2
2 0
4 1
6 3
1 0
2 1
4 1
1234 4
52 1
110 1
520 0
999 1
114514 0
3
12
32
27109943
596672839

Hint

[Sample Explanation #1]

In the first test case, the sequences that allow Alice to win are 110110, 101101, and 011011, for a total of 33.

In the second test case, the sequences that allow Alice to win are 1010010100, 1110011100, 1011010110, 1111011110, 1000110001, 1100111001, 0010100101, 0110101101, 1001110011, 1101111011, 0011100111, and 0111101111, for a total of 1212.

For the sequence 1110011100, after Bob finishes casting spells it becomes 1011010110. Alice's first move can merge the fourth and fifth numbers, making the sequence become 10111011. It can be seen that no matter how Bob plays, Alice will win.

[Sample #2]

See game/game2.in and game/game2.ans in the attachments.

This sample satisfies the constraints of test points 565\sim6.

[Sample #3]

See game/game3.in and game/game3.ans in the attachments.

This sample satisfies the constraints of test points 101110\sim11.

[Sample #4]

See game/game4.in and game/game4.ans in the attachments.

This sample satisfies the constraints of test points 121312\sim13.

[Sample #5]

See game/game5.in and game/game5.ans in the attachments.

This sample satisfies the constraints of test points 182018\sim20.

[Constraints]

For all testdata, it is guaranteed that: 1T5×1051\le T\le5\times 10^5, 1n10151\le n\le 10^{15}, 0mn0\le m\le n, m5×105\sum m \le5\times 10^5, 1ain1\le a_i\le n, ai<ai+1a_i < a_{i + 1}, vi{0,1}v_i\in\{0,1\}.

Test Point ID TT\le nn\le m\sum m\le Special Property
11 4040 2020 4040 None
242\sim4 10310^3 5×1025\times10^2 5×1055\times10^5 A
565\sim6 B
797\sim9 C
101110\sim11 2×1052\times 10^5 10610^6 00 None
121312\sim13 2×1052\times10^5
1414 2×1032\times 10^3 101510^{15} 00
151615\sim16 2×1032\times10^3
1717 5×1055\times 10^5 00
182018\sim20 5×1055\times10^5
  • Special property A: n=mn = m.
  • Special property B: n=mn = m, and vi=1v_i=1.
  • Special property C: n=mn = m, and there are no two adjacent 00 in vv, i.e. vi+vi+10v_i + v_{i + 1} \ne 0.

Translated by ChatGPT 5