#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;

const int MaxN = 19;
const int MaxM = 20;
const int M = 1000000007;

int n, m, nK;
int rest[MaxM + 1][MaxN];

int f[1 << MaxN];

int main()
{
	cin >> n >> m >> nK, --n;

	for (int i = 1; i <= m; ++i)
		for (int j = 0; j < n; ++j)
			rest[i][j] = -1;

	for (int i = 0; i < nK; ++i)
	{
		int u, v, w;
		cin >> u >> v >> w, --v;

		rest[u][v] = w;
	}

	f[0] = 1;
	for (int i = 1; i <= m; ++i)
		for (int j = 0; j < n; ++j)
			for (int s = 0; s < 1 << n; ++s)
			{
				if (f[s] == 0)
					continue;

				int cur = f[s];

				if (rest[i][j] == 1 || s >> j & 1)
					f[s] = 0;

				if (rest[i][j])
				{
					int p = s >> j;
					int t = s ^ (((p & -p) ^ 1) << j);
					f[t] = (f[t] + cur) % M;
				}
			}

	int res = 0;
	for (int s = 0; s < 1 << n; ++s)
		res = (res + f[s]) % M;

	cout << res << endl;

	return 0;
}