F - Zigzag Editorial /

Time Limit: 4 sec / Memory Limit: 256 MB

配点 : 16001600

問題文

下図のように,11 辺が NN 個の点からなる正三角形状に,N(N+1)/2N(N+1)/2 個の点が並んでいます. 上から ii 段目の左から jj 番目の点を (i,j)(i, j) で表すことにします(1iN1 \leq i \leq N, 1ji1 \leq j \leq i). また,(i+1,j)(i+1, j)(i,j)(i, j) のすぐ左下の点,(i+1,j+1)(i+1, j+1)(i,j)(i, j) のすぐ右下の点と呼ぶことにします.

高橋君は,この点を結んで,MM 個の折れ線 1,2,...,M1, 2, ..., M を描くことにしました. 各折れ線は,(1,1)(1, 1) から始めて,「現在いる点のすぐ左下の点かすぐ右下の点を選び,現在いる点と選んだ点を直線で結んだのち,選んだ点へ移動する」 ことを N1N-1 回繰り返すことで得られます. すなわち,ある Xi,1,...,Xi,NX_{i,1}, ..., X_{i,N} が存在して,次が成り立ちます:

  • 折れ線 ii は,NN 個の点 (1,Xi,1),(2,Xi,2),...,(N,Xi,N)(1, X_{i,1}), (2, X_{i,2}), ..., (N, X_{i,N}) をこの順で結んでいる.
  • j=1,2,...,N1j=1, 2, ..., N-1 に対して,Xi,j+1=Xi,jX_{i,j+1} = X_{i,j} または Xi,j+1=Xi,j+1X_{i,j+1} = X_{i,j}+1 が成り立つ.

高橋君は,折れ線 i+1i+1 が折れ線 ii の左に来ることはないように折れ線を描きたいです. つまり,j=1,2,...,Nj=1, 2, ..., N に対して,X1,jX2,j...XM,jX_{1,j} \leq X_{2,j} \leq ... \leq X_{M,j} が成り立ちます.

また,高橋君は,折れ線の曲がり方について KK 個の条件を満たすように折れ線を描かなければなりません. ii 番目の条件は (Ai,Bi,Ci)(A_i, B_i, C_i) で表され,これは次を意味します:

  • Ci=0C_i=0 のときは,折れ線 AiA_i を描くとき,BiB_i 回目の移動においては,その時いる点のすぐ左下の点を選ぶ.
  • Ci=1C_i=1 のときは,折れ線 AiA_i を描くとき,BiB_i 回目の移動においては,その時いる点のすぐ右下の点を選ぶ.

すなわち,XAi,Bi+1=XAi,Bi+CiX_{A_i, {B_i}+1} = X_{A_i, B_i} + C_i を意味します.

高橋君が MM 個の折れ線を描く方法が何通りあるかを mod 10000000071000000007 で求めてください.

注意

提出を行う前に,「コードテスト」を利用して実行時間を計測することを強く推奨する.

制約

  • 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=0,1C_i = 0,1
  • (Ai,Bi)(A_i, B_i) として同じ組は複数回与えられない.

入力

入力は以下の形式で標準入力から与えられる。

NN MM KK
A1A_1 B1B_1 C1C_1
A2A_2 B2B_2 C2C_2
:
AKA_K BKB_K CKC_K

出力

高橋君が MM 個の折れ線を描く方法は何通りあるかを mod 10000000071000000007 で出力せよ.


入力例 1Copy

Copy
3 2 1
1 2 0

出力例 1Copy

Copy
6

下図に示すように,66 通りの描き方があります.ただし,赤線は折れ線 11,緑線は折れ線 22 を表します:


入力例 2Copy

Copy
3 2 2
1 1 1
2 1 0

出力例 2Copy

Copy
0

入力例 3Copy

Copy
5 4 2
1 3 1
4 2 0

出力例 3Copy

Copy
172

入力例 4Copy

Copy
20 20 0

出力例 4Copy

Copy
881396682

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.


Sample Input 1Copy

Copy
3 2 1
1 2 0

Sample Output 1Copy

Copy
6

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


Sample Input 2Copy

Copy
3 2 2
1 1 1
2 1 0

Sample Output 2Copy

Copy
0

Sample Input 3Copy

Copy
5 4 2
1 3 1
4 2 0

Sample Output 3Copy

Copy
172

Sample Input 4Copy

Copy
20 20 0

Sample Output 4Copy

Copy
881396682


2025-03-30 (Sun)
22:49:01 +00:00