#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

const int MOD = 1e9 + 7; // change if needed

struct Modint {
  ll val;
  
  Modint (ll _val = 0)
    : val(_val % MOD) {}

  Modint operator+ (Modint other) const {
    return Modint(val + other.val);
  }

  void operator+= (Modint other) {
    val += other.val;
    val %= MOD;
  }

  Modint operator- () const {
    return Modint(MOD - val);
  }

  Modint operator- (Modint other) const {
    return Modint(val + MOD - other.val);
  }

  void operator-= (Modint other) {
    val += MOD - other.val;
    val %= MOD;
  }

  Modint operator* (Modint other) const {
    return Modint(val * other.val);
  }

  void operator*= (Modint other) {
    val *= other.val;
    val %= MOD;
  }

  bool operator== (Modint other) const {
    return val == other.val;
  }

  bool operator!= (Modint other) const {
    return val != other.val;
  }
};

Modint exp (Modint a, int k) {
  if (k == 0) {
    return Modint(1);
  } else if (k % 2 == 0) {
    Modint half = exp(a, k / 2);
    return half * half;
  } else {
    return a * exp(a, k - 1);
  }
}

Modint inv (Modint a) {
  return exp(a, MOD - 2);
}

ostream& operator<< (ostream& out, Modint p) {
  out << p.val;
  return out;
}

const int MAX_N = 19;
const int MAX_M = 25;

int constrl [MAX_M];
int constrr [MAX_M];

Modint dp [MAX_M][1 << MAX_N];
// dp[i][mask] is the # of ways to place i paths
// such that the i-th path is mask

Modint dpa [1 << MAX_N][MAX_N];

int main () {
  ios::sync_with_stdio(false);

  int n, m, K;
  cin >> n >> m >> K;
  n--;

  for (int i = 1; i <= m; i++) {
    constrr[i] = (1 << n) - 1;
  }

  for (int i = 0; i < K; i++) {
    int a, b, c;
    cin >> a >> b >> c;

    b = n - b;
    if (c == 0) {
      constrr[a] -= 1 << b;
    } else {
      constrl[a] += 1 << b;
    }
  }

  dp[0][0] = Modint(1);

  for (int i = 1; i <= m; i++) {
    // reset the dpa table
    for (int mask = 0; mask < 1 << n; mask++) {
      for (int k = 0; k < n - 1; k++) {
        dpa[mask][k] = Modint(0);
      }
      dpa[mask][n - 1] = dp[i - 1][mask];
    }

    // "sum-over-premasks"
    for (int mask = 0; mask < 1 << n; mask++) {
      for (int k = n - 1; k >= 0; k--) {
        if (k != n - 1) {
          if ((mask & 1 << k) && !(mask & 1 << (k + 1))) {
            int filter = (1 << k) | (1 << (k + 1));
            dpa[mask ^ filter][k + 1] += dpa[mask][k];
          }
        }

        if (k == 0) {
          dp[i][mask] += dpa[mask][k];
        } else {
          dpa[mask][k - 1] += dpa[mask][k];
        }

        int suff = (1 << (k + 1)) - 1;
        if (!(mask & suff)) {
          int nxt = mask | 1 << k;
          if (k == 0) {
            dp[i][nxt] += dpa[mask][k];
          } else {
            dpa[nxt][k - 1] += dpa[mask][k];
          }
        }
      }
    }

    /*
    cerr << i << endl;
    for (int mask = 0; mask < 1 << n; mask++) {
      cerr << " " << mask << ": ";
      for (int k = 0; k < n; k++) {
        cerr << dpa[mask][k] << " ";
      }
      cerr << endl;
    }
    */
    
    // delete the forbidden paths;
    for (int mask = 0; mask < 1 << n; mask++) {
      if ((constrl[i] | mask) != mask) {
        dp[i][mask] = 0;
      } else if ((constrr[i] & mask) != mask) {
        dp[i][mask] = 0;
      }

      //  cerr << i << " " << mask << " " << dp[i][mask] << endl;
    }
    //  cerr << endl;
  }

  Modint ans (0);
  for (int mask = 0; mask < 1 << n; mask++) {
    ans += dp[m][mask];
  }
  cout << ans << endl;
}


Measured memory_bytes: 183824