atcoder#AGC017F. [AGC017F] Zigzag

[AGC017F] Zigzag

Score : 16001600 points

Problem Statement

There are N(N+1)/2N(N+1)/2 dots arranged to form an equilateral triangle whose sides consist of NN dots, as shown below. The jj-th dot from the left in the ii-th row from the top is denoted by (i,j)(i, j) (1iN1 \leq i \leq N, 1ji1 \leq j \leq i). Also, we will call (i+1,j)(i+1, j) immediately lower-left to (i,j)(i, j), and (i+1,j+1)(i+1, j+1) immediately lower-right to (i,j)(i, j).

Takahashi is drawing MM polygonal lines L1,L2,...,LML_1, L_2, ..., L_M by connecting these dots. Each LiL_i starts at (1,1)(1, 1), and visits the dot that is immediately lower-left or lower-right to the current dots N1N-1 times. More formally, there exist Xi,1,...,Xi,NX_{i,1}, ..., X_{i,N} such that:

  • LiL_i connects the NN points (1,Xi,1),(2,Xi,2),...,(N,Xi,N)(1, X_{i,1}), (2, X_{i,2}), ..., (N, X_{i,N}), in this order.
  • For each j=1,2,...,N1j=1, 2, ..., N-1, either Xi,j+1=Xi,jX_{i,j+1} = X_{i,j} or Xi,j+1=Xi,j+1X_{i,j+1} = X_{i,j}+1 holds.

Takahashi would like to draw these lines so that no part of Li+1L_{i+1} is to the left of LiL_{i}. That is, for each j=1,2,...,Nj=1, 2, ..., N, X1,jX2,j...XM,jX_{1,j} \leq X_{2,j} \leq ... \leq X_{M,j} must hold.

Additionally, there are KK conditions on the shape of the lines that must be followed. The ii-th condition is denoted by (Ai,Bi,Ci)(A_i, B_i, C_i), which means:

  • If Ci=0C_i=0, LAiL_{A_i} must visit the immediately lower-left dot for the BiB_i-th move.
  • If Ci=1C_i=1, LAiL_{A_i} must visit the immediately lower-right dot for the BiB_i-th move.

That is, XAi,Bi+1=XAi,Bi+CiX_{A_i, {B_i}+1} = X_{A_i, B_i} + C_i must hold.

In how many ways can Takahashi draw MM polygonal lines? Find the count modulo 10000000071000000007.

Notes

Before submission, it is strongly recommended to measure the execution time of your code using "Custom Test".

Constraints

  • 1N201 \leq N \leq 20
  • 1M201 \leq M \leq 20
  • 0K(N1)M0 \leq K \leq (N-1)M
  • 1AiM1 \leq A_i \leq M
  • 1BiN11 \leq B_i \leq N-1
  • Ci=0C_i = 0 or 11
  • No pair appears more than once as (Ai,Bi)(A_i, B_i).

Input

Input is given from Standard Input in the following format:

NN MM KK

A1A_1 B1B_1 C1C_1

A2A_2 B2B_2 C2C_2

:

AKA_K BKB_K CKC_K

Output

Print the number of ways for Takahashi to draw MM polygonal lines, modulo 10000000071000000007.

3 2 1
1 2 0
6

There are six ways to draw lines, as shown below. Here, red lines represent L1L_1, and green lines represent L2L_2.

3 2 2
1 1 1
2 1 0
0
5 4 2
1 3 1
4 2 0
172
20 20 0
881396682