QOJ.ac

QOJ

時間限制: 1.0 s 記憶體限制: 512 MB 總分: 100

#80. TRAX

统计

Snuke plays with the following tile (this tile is used in a game called TRAX):

problem_80_4eeece2ca1222b7827e8b9f1e999d863.jpg

Snuke wants to fill an $H \times W$ grid with these tiles. In each cell of the grid, he places a tile in one of the four orientations:

problem_80_38e028b9ff5f0a5f6b9fecb9bc2f8714.jpg

These orientations are numbered 1 through 4 as in the picture above.

The placement of tiles must satisfy the following constraints:

  • A red arc must not touch a white arc.
  • The grid must not contain cycles (see the examples below).
  • For each $i$ ($1 \le i \le N$), in the cell that is $R_i$-th from the top and $C_i$-th from the left, the orientation of the tile must be $D_i$.

For example, the following picture shows two invalid placements of tiles. The placement on the left contains a white cycle, and the placement on the right contains a red cycle and a white large cycle.

problem_80_3fbc167233e90ba04a1a558c676776b5.jpg

Count the number of valid placements, modulo $10^9 + 7$.

Input

$$ \begin{gathered} H\ W\\ N\\ R_1\ C_1\ D_1\\ \vdots\\ R_N\ C_N\ D_N \end{gathered} $$

  • $1 \le H,W \le 10^5$
  • $0 \le N \le 10^5$
  • $1 \le R_i \le H$
  • $1 \le C_i \le W$
  • $1 \le D_i \le 4$
  • $(R_i,C_i)$ are pairwise distinct.

Output

Print the number of valid placements, modulo $10^9 + 7$.

Examples

Input 1

2 2
1
1 1 4

Output 1

4

Input 2

2 10
2
1 1 1
1 2 1

Output 2

0

Input 3

2015 1114
0

Output 3

711460824

Input 4

2 2
2
1 1 1
2 2 3

Output 4

0

Input 5

5 6
3
1 2 2
4 1 1
5 6 4

Output 5

12

Input 6

5 6
2
3 3 4
3 4 2

Output 6

39

Note

For sample 1, these are the four valid placements:

problem_80_73c7bb2f67bf00c1d2a9ca78798611b7.jpg

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#495Editorial Open题解Milmon Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.