#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <iostream>
#include <functional>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <bitset>

using namespace std;
typedef pair<int, int> Pi;
typedef long long ll;
#define pii Pi
#define pll PL
#define Fi first
#define Se second
#define pb(x) push_back(x)
//#define sz(x) ((int)(x).size())
#define rep(i, n) for(int i=0;i<n;i++)
#define all(x) (x).begin(), (x).end()
typedef tuple<int, int, int> t3;
typedef pair<ll, ll> pll;
typedef long double ldouble;
typedef pair<double, double> pdd;

const double PI = acos(-1);
const double EPS = 1e-9;

const int MX = 1<<20;
const int MM = 1000000007;

int T[MX];
int msk[MX], val[MX];
int N, M, K;

int nxt(int i, int v){
	return ((i + (1<<v)) | (i^1<<v)) & ((1<<N)-1);
}

int main()
{
	scanf("%d%d%d", &N, &M, &K); N--;
	for(int i = 1; i <= K; i++){
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		msk[a] |= 1<<b-1;
		val[a] |= c<<b-1;
	}

	for(int i = 0; i < 1<<N; i++){
		if( val[1] == (msk[1]&i)) T[i] = 1;
	}
	for(int t = 2; t <= M; t++){
		for(int v = N-1; v >= 0; v--){
			for(int i = 0; i < 1<<N; i++){
				if( i&1<<v ) T[i] = (T[i] + T[nxt(i, v)]) % MM;
			}
		}
		
		for(int i = 0; i < 1<<N; i++){
			if( val[t] != (msk[t]&i) ) T[i] = 0;
		}
	}
	int ans = 0;
	for(int i = 0; i < 1<<N; i++) ans = (ans + T[i]) % MM;
	printf("%d\n", ans);
}
