Hide

Problem H
Plus Minus

Matthew the physicist studies the quantum electro-dynamics of a silicon-based rectangular microchip. The microchip consists of a very large $N \times M$ grid of electrons. Each electron has either positive (up) or negative (down) spin, denoted by $+$ and $-$ respectively.

Matthew does not know the spin of all the electrons, but he has done $K$ measurements. In the $i$-th measurement, he discovered that the electron at position $(y_ i, x_ i)$ has a given spin $s_ i$. He also knows that in each $2\times 2$ subgrid, there are equally many electrons with positive and negative spin. He wants to know whether he can recover the state of every electron based on his measurements. If not, he would like to know how many possible states are consistent with his measurements. For classified reasons, he wants the answer modulo $10^9 + 7$.

Input

The first line contain three numbers $N$, $M$ and $K$: the height of the grid, the width of the grid and the number of measurements. The next $K$ lines contain a spin $s_ i$ where $s_ i$ is either $+$ or $-$, and two numbers $1 \leq y_ i \leq N$ and $1 \leq x_ i \leq M$ – the coordinates of the electron. Matthew never did two measurments at the exact same location.

We always have $1 \leq N, M \leq 10^9$ and $0 \leq K \leq 100\, 000$.

Output

Output the total number of valid states consistent with Matthew’ measurements modulo $10^9 + 7$.

Sample Input 1 Sample Output 1
2 4 4
+ 1 1
- 1 2
+ 1 3
- 1 4
2
Sample Input 2 Sample Output 2
3 3 3
- 2 1
+ 2 3
+ 3 3
0

Please log in to submit a solution to this problem

Log in