Turtle Magic: Royal Turtle Shell Pattern
Contest: Codeforces Round 929 (Div. 3)
Difficulty rating: 2300
Tags: bitmasks, brute force, combinatorics, constructive algorithms, dfs and similar, math

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 \times m$$$ grid ($$$n, m \ge 5$$$), where the rows are numbered $$$1, 2, \dots, n$$$ and columns are numbered $$$1, 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 $$$a$$$-th row and the $$$b$$$-th column is denoted as $$$(a, b)$$$.

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

Before all operations and after each of the $$$q$$$ 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)$$$ satisfying $$$1 \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)$$$.
- There does not exist any $$$(i,j)$$$ satisfying $$$1 \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)$$$.
- There does not exist any $$$(i,j)$$$ satisfying $$$1 \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)$$$.
- There does not exist any $$$(i,j)$$$ satisfying $$$1 \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)$$$.

You should output all answers modulo $$$998\,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 $$$0$$$.

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

The first line of each test case contains three integers $$$n$$$, $$$m$$$, $$$q$$$ ($$$5 \le n, m \le 10^9, 0 \le q \le \min(n \times m, 10^5)$$$).

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

The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$.

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

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

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

Example 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

Example output:
8
4
3
1
0
8
4
1
0

Editorial:
We claim that there are only $$$8$$$ configurations that satisfy the condition. The proof is as follows.
Firstly, consider a $$$2 \times 2$$$ subgrid that does not lie on any of the grid's corners.
Claim 1. Within the $$$2 \times 2$$$ subgrid, there must be 2 Os and 2 Xs.
Proof of Claim 1. Assume the contrary that there are 3 Os or 4 Os. (The case with 3 Xs or 4 Xs is done too due to symmetry.)
Then, after some logical deduction we will end up with 3 consecutive Xs, as illustrated in the following figure.

The condition is not satisfied, hence there is a contradiction and there must be 2 Os and 2 Xs within a $$$2 \times 2$$$ subgrid that does not lie on any of the grid's corners.
Next, consider a $$$3 \times 3$$$ subgrid that does not lie on any of the grid's corners.
Claim 2. Within the $$$3 \times 3$$$ subgrid, at least one $$$2 \times 2$$$ sub-subgrid is one of the four patterns below. We will call the following $$$2 \times 2$$$ patterns good patterns.

Proof of Claim 2. Assume the contrary that none of the 4 sub-subgrids in the $$$3 \times 3$$$ subgrid are one of the four given patterns. This naturally means every two cells which share an edge have different shapes. This gives the following two patterns:

The diagonals have the same shape, so the condition is not satisfied. There is a contradiction, hence the claim is true.
Next, consider a good $$$2 \times 2$$$ subgrid that does not lie on any of the grid's corners. (We have proved its existence in Claim 2.)
Finally, it is possible to uniquely extend a good subgrid to the rest of the grid. For example, you can see in the illustration below, after a few unique logical deductions the grey pattern on the top can tesselate itself a few times, and this can be infinitely repeated.

Therefore, the entire grid must be tesselations of one of the four good patterns. The first two patterns may be shifted one column to the right, and the last two patterns may be shifted one column downwards. So there are a total of $$$4 \times 2 = 8$$$ ways to satisfy the condition.
Specifically, the $$$8$$$ ways for $$$n = 5, m =5$$$ are as follows:

As for some implementation details, the following $$$8$$$ statements each correspond to one $$$n \times m$$$ configuration that satisfies the condition. $$$a_{i,j}$$$ represents whether the cell on the $$$i$$$-th row and the $$$j$$$-th column ($$$1 \le i \le n, 1 \le j \le m$$$) has a circle-shaped fortune cookie.
1. $$$a_{i,j} = 1$$$ iff $$$i + \left \lceil \frac{j}{2} \right \rceil$$$ is odd.
2. $$$a_{i,j} = 1$$$ iff $$$i + \left \lceil \frac{j}{2} \right \rceil$$$ is even.
3. $$$a_{i,j} = 1$$$ iff $$$i + \left \lfloor \frac{j}{2} \right \rfloor$$$ is odd.
4. $$$a_{i,j} = 1$$$ iff $$$i + \left \lfloor \frac{j}{2} \right \rfloor$$$ is even.
5. $$$a_{i,j} = 1$$$ iff $$$j + \left \lceil \frac{i}{2} \right \rceil$$$ is odd.
6. $$$a_{i,j} = 1$$$ iff $$$j + \left \lceil \frac{i}{2} \right \rceil$$$ is even.
7. $$$a_{i,j} = 1$$$ iff $$$j + \left \lfloor \frac{i}{2} \right \rfloor$$$ is odd.
8. $$$a_{i,j} = 1$$$ iff $$$j + \left \lfloor \frac{i}{2} \right \rfloor$$$ is even.
Discussion: Try to solve the problem for general $$$n, m$$$. Specifically that includes the cases when $$$1 \le \min(n, m) \le 4$$$.