#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using vi = vector<int>;
using db = double;
#define pb push_back

const ll mod = 1000000007;

int n, m, q;
ll f[1 << 19];
int msk[21];
int asn[21];
int u;

bool leg(int x, int s) {
	return (s & msk[x]) == asn[x];
}

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

int main() {
	cin >> n >> m >> q;
	if (n == 1) {
		cout << 1 % mod << endl;
		return 0;
	}
	
	--n;
	u = 1 << n;
	
	for (int i = 0; i < q; ++i) {
		int a, b, c;
		cin >> a >> b >> c;
		msk[a] |= 1 << b - 1;
		asn[a] |= c << b - 1;
	}
	
	
	f[0] = 1;
	for (int i = 1; i <= m; ++i) {
		for (int k = 0; k < n; ++k)
			for (int j = 0; j < u; ++j) if (~j >> k & 1)
				(f[j + (1 << k) - lowbit(j & ~((1 << k) - 1))] += f[j]) %= mod;
		for (int j = 0; j < u; ++j) if (!leg(i, j))
			f[j] = 0;
	}
	
	ll ans = 0;
	for (int i = 0; i < u; ++i)
		ans += f[i];
	
	cout << ans % mod << endl;
	
	return 0;
}
