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

<image>

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

* L_i connects the N points (1, X_{i,1}), (2, X_{i,2}), ..., (N, X_{i,N}), in this order.
* For each j=1, 2, ..., N-1, either X_{i,j+1} = X_{i,j} or X_{i,j+1} = X_{i,j}+1 holds.



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

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

* If C_i=0, L_{A_i} must visit the immediately lower-left dot for the B_i-th move.
* If C_i=1, L_{A_i} must visit the immediately lower-right dot for the B_i-th move.



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

In how many ways can Takahashi draw M polygonal lines? Find the count modulo 1000000007.

Constraints

* 1 \leq N \leq 20
* 1 \leq M \leq 20
* 0 \leq K \leq (N-1)M
* 1 \leq A_i \leq M
* 1 \leq B_i \leq N-1
* C_i = 0 or 1
* No pair appears more than once as (A_i, B_i).

Input

Input is given from Standard Input in the following format:


N M K
A_1 B_1 C_1
A_2 B_2 C_2
:
A_K B_K C_K


Output

Print the number of ways for Takahashi to draw M polygonal lines, modulo 1000000007.

Examples

Input

3 2 1
1 2 0


Output

6


Input

3 2 2
1 1 1
2 1 0


Output

0


Input

5 4 2
1 3 1
4 2 0


Output

172


Input

20 20 0


Output

881396682

#include <cstdio>
#include <cstring>

using namespace std;
const int MOD(1000000007);
const int Max_N(20);
const int Max_M(25);
typedef long long int LL;

constexpr int Mult(int a, int b)
{
	return a * 1LL * b % MOD;
}

constexpr int Add(int a, int b)
{
	return a + b >= MOD ? a + b - MOD : a + b;
}

constexpr int Sub(int a, int b)
{
	return a - b < 0 ? a - b + MOD : a - b;
}

inline int lowbit(int x)
{
	return x & -x;
}

/*
按照编号从小到大的顺序考虑每条路径，每条路径必须在上一条路径的右边。设F[i][S]表示考虑到第i条路径，第i条路径是S的方案数
转移考虑第i + 1条的路径是什么。设第i条路径是S1，第i + 1条路径是S2，需要满足对于任意前缀，S1的1的个数 <= S2的1的个数
这个条件等价于：每一个S1中的1都可以找到S2中在其位置之前的1匹配。可以理解成hall定理的逆命题 
考虑dp这个过程，设G[h][S]表示考虑到第h层，S2[1 ~ h] = S[1 ~ h]，S1[h + 1 ~ N]中没被匹配的1 = S[h + 1 ~ N]
每次转移考虑S2[h + 1]是什么。分情况讨论：
(1)S2[h + 1] = 0，需要保证S1中不能有无法被匹配的1
(2)S2[h + 1] = 1，那么贪心把S1中没有被匹配的最靠前的1与其匹配
G直接转移即可，F的转移也就很方便了
*/

int N, M, K, F[1 << (Max_N - 1)], G[2][1 << (Max_N - 1)], X[Max_M][Max_N];

int main()
{
	scanf("%d%d%d", &N, &M, &K), --N, memset(X, -1, sizeof(X));
	for (int a, b;K--;)
		scanf("%d%d", &a, &b), scanf("%d", &X[a][b - 1]);
	F[0] = 1;
	for (int i = 1;i <= M;++i)
	{
		memset(G[1], 0, sizeof(G[1]));
		for (int S = 0;S < (1 << N);++S)
			G[1][S] = F[S];
		for (int b = 0, now = 1, lst = 0;b <= N - 1;++b)
		{
			now ^= 1, lst ^= 1, memset(G[now], 0, sizeof(G[now]));
			for (int S = 0, S0;S < (1 << N);++S)
				for (int c = 0;c <= 1;++c)
				{
					if (X[i][b] != -1 && X[i][b] != c)
						continue;
					S0 = S;
					if (c)
						S0 = S0 - lowbit(S0 - (S0 & ((1 << b) - 1)));
					if (S0 & (1 << b))
						continue;
					S0 |= (c * (1 << b));
					G[now][S0] = Add(G[now][S0], G[lst][S]);
				}
		}
		for (int S = 0;S < (1 << N);++S)
			F[S] = G[(N - 1) & 1][S];
	}
	int Ans(0);
	for (int S = 0;S < (1 << N);++S)
		Ans = Add(Ans, F[S]);
	printf("%d", Ans);
	return 0;
}