CF BUDDY
← Problems·

1933G · Turtle Magic: Royal Turtle Shell Pattern

2300 · bitmasks, brute force, combinatorics

Problem: Turtle Alice is currently designing a fortune cookie box, and she would like to incorporate the theory of LuoShu into it.

The box can be seen as an n×mn \times m grid (n,m5n, m \ge 5), where the rows are numbered 1,2,,n1, 2, \dots, n and columns are numbered 1,2,,m1, 2, \dots, m. Each cell can either be empty or have a single fortune cookie of one of the following shapes: circle or square. The cell at the intersection of the aa-th row and the bb-th column is denoted as (a,b)(a, b).

Initially, the entire grid is empty. Then, Alice performs qq operations on the fortune cookie box. The ii-th operation (1iq1 \le i \le q) is as follows: specify a currently empty cell (ri,ci)(r_i,c_i) and a shape (circle or square), then put a fortune cookie of the specified shape on cell (ri,ci)(r_i,c_i). Note that after the ii-th operation, the cell (ri,ci)(r_i,c_i) is no longer empty.

Before all operations and after each of the qq operations, Alice wonders what the number of ways to place fortune cookies in all remaining empty cells is, such that the following condition is satisfied:

No three consecutive cells (in horizontal, vertical, and both diagonal directions) contain cookies of the same shape. Formally:

  • There does not exist any (i,j)(i,j) satisfying 1in,1jm21 \le i \le n, 1 \le j \le m-2, such that there are cookies of the same shape in cells (i,j),(i,j+1),(i,j+2)(i,j), (i,j+1), (i,j+2).
  • There does not exist any (i,j)(i,j) satisfying 1in2,1jm1 \le i \le n-2, 1 \le j \le m, such that there are cookies of the same shape in cells (i,j),(i+1,j),(i+2,j)(i,j), (i+1,j), (i+2,j).
  • There does not exist any (i,j)(i,j) satisfying 1in2,1jm21 \le i \le n-2, 1 \le j \le m-2, such that there are cookies of the same shape in cells (i,j),(i+1,j+1),(i+2,j+2)(i,j), (i+1,j+1), (i+2,j+2).
  • There does not exist any (i,j)(i,j) satisfying 1in2,1jm21 \le i \le n-2, 1 \le j \le m-2, such that there are cookies of the same shape in cells (i,j+2),(i+1,j+1),(i+2,j)(i,j+2), (i+1,j+1), (i+2,j).

You should output all answers modulo 998244353998\,244\,353. Also note that it is possible that after some operations, the condition is already not satisfied with the already placed candies, in this case you should output 00.

Input Format: The first line of the input contains a single integer tt (1t1031 \le t \le 10^3) — the number of test cases.

The first line of each test case contains three integers nn, mm, qq (5n,m109,0qmin(n×m,105)5 \le n, m \le 10^9, 0 \le q \le \min(n \times m, 10^5)).

The ii-th of the next qq lines contains two integers rir_i, cic_i and a single string shapei\text{shape}_i (1rin,1cim1 \le r_i \le n, 1 \le c_i \le m, shapei=\text{shape}_i= "circle" or "square"), representing the operations. It is guaranteed that the cell on the rir_i-th row and the cic_i-th column is initially empty. That means, each (ri,ci)(r_i,c_i) will appear at most once in the updates.

The sum of qq over all test cases does not exceed 10510^5.

Output Format: For each test case, output q+1q+1 lines. The first line of each test case should contain the answer before any operations. The ii-th line (2iq+12 \le i \le q+1) should contain the answer after the first i1i-1 operations. All answers should be taken modulo 998244353998\,244\,353.

Note: In the second sample, after placing a circle-shaped fortune cookie to cells (1,1)(1,1), (1,2)(1,2) and (1,3)(1,3), the condition is already not satisfied. Therefore, you should output 00.

Sample Cases

Case 1

Input

2
6 7 4
3 3 circle
3 6 square
5 3 circle
5 4 square
5 5 3
1 1 circle
1 2 circle
1 3 circle

Output

8
4
3
1
0
8
4
1
0

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback