#include <bits/stdc++.h>
#define ctz __builtin_ctz
#define EB emplace_back

typedef std::pair <int, int> pr;
typedef std::vector <pr> vector;
const int N = 24, N2 = 524354, mod = 1000000007;

int L, n, q, ALL;
int mask0[N], mask1[N], f[N2];
vector mdf[N];

inline void add(int &x, const int y) {x += y - mod, x += x >> 31 & mod;}

int main() {
	int i, j, c, S;
	scanf("%d%d%d", &L, &n, &q), ALL = ~(-1 << --L);
	for (; q; --q) scanf("%d%d%d", &i, &j, &c), (c ? mask1 : mask0)[--i] |= 1 << (L - j);
	for (i = 0; i <= ALL; ++i)
		for (c = 0, S = i & ((ALL & ~i) >> 1); S; S &= S - 1, ++c)
			 mdf[c].EB(i, i ^ ((S & -S) * 3));
	for (*f = 1, i = 0; i < n; ++i) {
		for (j = 0; j < ALL; ++j) add(f[j | (j + 1)], f[j]);
		for (c = 0; c < L; ++c) for (const pr e : mdf[c]) add(f[e.second], f[e.first]);
		for (j = 0; j <= ALL; ++j) if ((j & mask0[i]) | (~j & mask1[i])) f[j] = 0;
	}
	printf("%d\n", int(std::accumulate(f, f + (ALL + 1), 0ll) % mod));
	return 0;
}