#define DEB
#include<bits/stdc++.h>
#define REP(i,m) for(int i=0;i<(m);++i)
#define REPN(i,m,in) for(int i=(in);i<(m);++i)
#define ALL(t) (t).begin(),(t).end()
#define CLR(a) memset((a),0,sizeof(a))
#define pb push_back
#define mp make_pair
#define fr first
#define sc second

using namespace std;


#ifdef DEB
#define dump(x)  cerr << #x << " = " << (x) << endl
#define prl cerr<<"called:"<< __LINE__<<endl
#define dumpR(x) cerr<<"\x1b[31m"<<#x<<" = " <<(x)<<"\x1b[39m"<<endl
#define dumpY(x) cerr<<"\x1b[33m"<<#x<<" = " <<(x)<<"\x1b[39m"<<endl
#define dumpG(x) cerr<<"\x1b[32m"<<#x<<" = " <<(x)<<"\x1b[39m"<<endl
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
#else
#define dump(x) ;
#define dumpR(x) ;
#define dumpY(x) ;
#define dumpG(x) ;
#define prl ;
template<class T> void debug(T a,T b){ ;}
#endif

template<class T> void chmin(T& a,const T& b) { if(a>b) a=b; }
template<class T> void chmax(T& a,const T& b) { if(a<b) a=b; }

typedef long long int lint;
typedef pair<int,int> pi;

namespace std{
  template<class S,class T>
  ostream &operator <<(ostream& out,const pair<S,T>& a){
    out<<'('<<a.fr<<','<<a.sc<<')';
    return out;
  }
}

//const int INF=5e8;

template<lint mod>
struct Int_{
  unsigned x;
  unsigned mpow(Int_ a,unsigned k){
    Int_ res=1;
    while(k){
      if(k&1) res=res*a;
      a=a*a;
      k>>=1;
    }
    return res.x;
  }
  unsigned inverse(Int_ a){
    return mpow(a,mod-2);
  }
  Int_(): x(0) { }
  Int_(long long sig) {
    int sigt=sig%mod;
    if(sigt<0) sigt+=mod;
    x=sigt;
  }
  unsigned get() const { return (unsigned)x; }
  
  Int_ &operator+=(Int_ that) { if((x += that.x) >= mod) x -= mod; return *this; }
  Int_ &operator-=(Int_ that) { if((x += mod - that.x) >= mod) x -= mod; return *this; }
  Int_ &operator*=(Int_ that) { x = (unsigned long long)x * that.x % mod; return *this; }
  Int_ &operator=(Int_ that) { x=that.x; return *this;}
  Int_ &operator/=(Int_ that) { x=(unsigned long long) x * inverse(that.x)%mod; return *this;}
  bool operator==(Int_ that) const { return x==that.x; }
  bool operator!=(Int_ that) const { return x!=that.x; }

  Int_ operator-() const { return Int_(0)-Int_(*this);}
  Int_ operator+(Int_ that) const { return Int_(*this) += that; }
  Int_ operator-(Int_ that) const { return Int_(*this) -= that; }
  Int_ operator*(Int_ that) const { return Int_(*this) *= that; }
  Int_ operator/(Int_ that) const { return Int_(*this) /= that; }
  Int_ inv(){
    return Int_(mpow(x,mod-2));
  }
};

namespace std{
  template<lint mod>
  ostream &operator <<(ostream& out,const Int_<mod>& a){
    out<<a.get();
    return out;
  }
  template<lint mod>
  istream &operator >>(istream& in,Int_<mod>& a){
    in>>a.x;
    return in;
  }
};

typedef Int_<1000000007> Int;

Int mpow(Int a,lint k){
  Int res=1;
  while(k){
    if(k&1) res=res*a;
    a=a*a;
    k>>=1;
  }
  return res;
}
int n,m,K;

Int dp[2][1<<19];
int mark[25][25];
int main(){
  memset(mark,-1,sizeof(mark));
  cin>>n>>m>>K;
  --n;
  REP(i,K){
    int a,b,c;cin>>a>>b>>c;
    --a;--b;
    mark[a][b]=c;
  }
  /*
  int tot=0;
  REP(t,2){
    REP(d,n){
      REP(x,d+1){
        dp[t][d][x].resize(1<<n);
        tot+=dp[t][d][x].size();
      }
    }
  }
  */
  int cur=0,nxt=1;

  dp[cur][0]=1;
  int all[25];
  REP(i,25) all[i]=(1<<i)-1;
  REP(i,m){
    REP(d,n){
      CLR(dp[nxt]);
      REP(bit,(1<<n)) if(dp[cur][bit].x){
        Int val=dp[cur][bit];
        if(mark[i][d]!=1 && (bit&1)==0){//t=0
          if(d==n-1){
            dp[nxt][bit>>1]+=val;
          }else{
            dp[nxt][bit>>1]+=val;
          }
        }
        if(mark[i][d]!=0){
          if(d==n-1){
            dp[nxt][(bit>>1)|(1<<(n-1))]+=val;
          }else if(bit&1){
            dp[nxt][(bit>>1)|(1<<(n-1))]+=val;
          }else{
            auto fix=[&](int x){
              return x-(x&-x);
            };
            dp[nxt][fix(bit>>1&all[n-d-1])|((bit>>1)&(all[n]^all[n-d-1]))|(1<<(n-1))]+=val;
          }
        }
      }
      swap(cur,nxt);
    }
  }
  Int res=0;
  REP(bit,(1<<n)) res+=dp[cur][bit];
  cout<<res<<endl;

  return 0;
}





Measured memory_bytes: 4352