#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dwn(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
inline int read() {
    int x=0,f=1;char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
    for(;isdigit(c);c=getchar()) x=x*10+c-'0';
    return x*f;
}
typedef long long ll;
const int maxn=21;
const int maxm=1<<20;
const int mod=1000000007;
int f[2][maxm],trans[maxm][maxn];
int g[maxn][maxn];
int n,m,k;
int main() {
	n=read();m=read();k=read();
	memset(g,-1,sizeof(g));
	rep(i,1,k) {
		int a=read()-1,b=read()-1;
		g[a][b]=read();
	}
	rep(S,0,(1<<n)-1) {
		rep(i,0,n-1) if(!(S>>i&1)) {
			int k=n;
			rep(j,i+1,n-1) if(S>>j&1) {k=j;break;}
			if(k==n) trans[S][i]=S^(1<<i);
			else trans[S][i]=S^(1<<i)^(1<<k);
		}
	}
	int cur=0;f[0][0]=1;
	rep(i,0,m-1) {
		rep(j,0,n-1) {
			cur^=1;
			memset(f[cur],0,sizeof(f[cur]));
			int ok1=(g[i][j]<0||g[i][j]==1),ok2=g[i][j]<=0;
			rep(S,0,(1<<n)-1) if(f[cur^1][S]) {
				if(ok1) {
					if(S>>j&1) (f[cur][S]+=f[cur^1][S])%=mod;
					else (f[cur][trans[S][j]]+=f[cur^1][S])%=mod;
				}
				if(ok2) {
					if(!(S>>j&1)) (f[cur][S]+=f[cur^1][S])%=mod;
				}
			}
		}
		cur^=1;
	}
	int ans=0;
	rep(S,0,(1<<n)-1) (ans+=f[cur][S])%=mod;
	printf("%d\n",ans);
  	return 0;
}
