#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int N=19;
const int M=20;
const int S=1<<N;
const int P=1000000007;

int res[M],val[M];
int f[2][N][S];
int n,m,K,ans,all;

inline void update(int &x,int y){(x+=y)%=P;}

inline void trans(int *tar,int *src,int id,int d)
{
	for (int s=0;s<=all;++s)
		if (src[s])
		{
			int bit=(s>>d)&1;
			for (int i=0;i<2;++i)
				if (!((res[id]>>d)&1)||!(((val[id]>>d)&1)^i))
					if (bit<i)
					{
						int rem=(s>>d+1)<<d+1,lb=rem&-rem;
						update(tar[s^lb^(1<<d)],src[s]);
					}
					else if (bit==i) update(tar[s],src[s]);
		}
}

void dp()
{
	for (int s_=all^res[0];s_;s_=(s_-1)&(all^res[0])) update(f[0][n-1][s_|val[0]],1);
	update(f[0][n-1][val[0]],1);
	for (int i=1,src,tar;i<m;++i)
	{
		tar=i&1,src=tar^1,memset(f[tar],0,sizeof f[tar]);
		trans(f[tar][0],f[src][n-1],i,0);
		for (int j=1;j<n;++j) trans(f[tar][j],f[tar][j-1],i,j);
	}
	ans=0;
	for (int s_=all^res[m-1];s_;s_=(s_-1)&(all^res[m-1])) update(ans,f[(m-1)&1][n-1][s_|val[m-1]]);
	update(ans,f[(m-1)&1][n-1][val[m-1]]);
}

int main()
{
	//freopen("zigzag.in","r",stdin),freopen("zigzag.out","w",stdout);
	scanf("%d%d%d",&n,&m,&K),--n,all=(1<<n)-1;
	for (int i=1,x,y,z;i<=K;++i) scanf("%d%d%d",&x,&y,&z),--x,--y,res[x]|=1<<y,val[x]|=(1<<y)*z;
	dp(),printf("%d\n",ans);
	//fclose(stdin),fclose(stdout);
	return 0;
}