#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
using namespace std;
const int maxn=20+10,mo=1000000007;
int dp[2][maxn][(1<<21)+10],nxt[(1<<21)+10][maxn];
int bz[maxn][maxn];
int i,j,k,l,r,s,t,n,m,p,ans,now;
int main(){
    scanf("%d%d%d",&n,&m,&p);
    n--;
    fo(i,0,(1<<n)-1){
        t=-1;
        fd(j,n-1,0){
            if ((i&(1<<j))) t=j;
            nxt[i][j]=t;
        }
    }
    fo(i,1,m)
        fo(j,0,n-1)
            bz[i][j]=-1;
    fo(i,1,p){
        scanf("%d%d%d",&j,&k,&l);
        bz[j][k-1]=l;
    }
    dp[0][n][0]=1;
    now=1;
    fo(i,1,m){
        fo(s,0,(1<<n)-1) dp[now][0][s]=dp[1-now][n][s];
        fo(j,0,n)
            fo(s,0,(1<<n)-1)
                dp[1-now][j][s]=0;
        fo(j,0,n-1)
            fo(s,0,(1<<n)-1)
                if (dp[now][j][s]){
                    fo(k,0,1){
                        if (bz[i][j]!=-1&&bz[i][j]!=k) continue;
                        t=(s&(1<<j));
                        if (t) t=1;
                        if (k==0&&t==1) continue;
                        if (k==t) (dp[now][j+1][s]+=dp[now][j][s])%=mo;
                        else{
                            r=nxt[s][j];
                            if (r==-1) (dp[now][j+1][s|(1<<j)]+=dp[now][j][s])%=mo;
                            else (dp[now][j+1][(s|(1<<j))-(1<<r)]+=dp[now][j][s])%=mo;
                        }
                    }
                }
        now=1-now;
    }
    fo(s,0,(1<<n)-1) (ans+=dp[1-now][n][s])%=mo;
    (ans+=mo)%=mo;
    printf("%d\n",ans);
}