#include <cstdio>
#include <cstring>

const int MOD = 1000000007;

const int N = 20 + 3;
const int M = 20 + 3;
const int K = (N - 1) * M;

const int T = 1 << 20;

int n, m, k;

int c[M][N];

int t;

int f[2][N][T];

int z = 0;

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

inline int full(int j) {
	return (1 << (j)) - 1;
}

inline int func(int s, int j) {
	int top = s & ~full(j);
	int bottom = s & full(j);
	return top - lowbit(top) + (1 << j) + bottom;
}

inline int get(int s, int j) {
	return s & (1 << j);
}

void solve() {
	f[0][0][0] = 1;
	for (int _i = 0; _i < m; _i++) {
		int i = _i % 2;
		for (int j = 0; j < n; j++) {
			for (int s = 0; s < t; s++) {
				if (f[i][j][s] == 0) continue;
//				printf("%d %d %d : %d %d\n", _i, j, s, f[i][j][s], c[_i][j]);
				if (c[_i][j] != 1 && get(s, j) == 0) {
					f[i][j + 1][s] += f[i][j][s];
					f[i][j + 1][s] %= MOD;
				}
				if (c[_i][j] != 0) {
					f[i][j + 1][func(s, j)] += f[i][j][s];
					f[i][j + 1][func(s, j)] %= MOD;
				}
			}
		}
		memset(f[(i + 1) % 2], 0, sizeof f[(i + 1) % 2]);
		for (int s = 0; s < t; s++)
			f[(i + 1) % 2][0][s] = f[i][n][s];
	}
}

int main(int argc, char *argv[]) {
	memset(c, -1, sizeof c);
	scanf("%d%d%d", &n, &m, &k);
	n--;
	t = 1 << n;
	for (int i = 0; i < k; i++) {
		int a, b, c_;
		scanf("%d%d%d", &a, &b, &c_);
		c[a - 1][b - 1] = c_;
	}
	solve();
	int ans = 0;
	for (int s = 0; s < t; s++) {
		ans += f[(m - 1) % 2][n][s];
		ans %= MOD;
	}
	printf("%d\n", ans);
	return 0;
}
