#include <bits/stdc++.h>

using namespace std;

const int md = 1000000007;

inline void add(int &a, int b) {
  a += b;
  if (a >= md) {
    a -= md;
  }
}

const int N = 1234567;

int need1[23], need2[23];
int go[N][23];
int put[N];
int f[N];

int main() {
  int n, m, k;
  scanf("%d %d %d", &n, &m, &k);
  for (int i = 0; i < m; i++) {
    need1[i] = 0;
    need2[i] = 0;
  }
  while (k--) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    a--; b--;
    need1[a] |= (1 << b);
    need2[a] |= (1 << b) * c;
  }
  n--;
  for (int t = 0; t < (1 << n); t++) {
    for (int i = 0; i < n; i++) {
      go[t][i] = -1;
    }
    int id = 0;
    for (int i = 0; i < n; i++) {
      if (t & (1 << i)) {
        if (i > 0 && !(t & (1 << (i - 1)))) {
          go[t][id] = t ^ (1 << i) ^ (1 << (i - 1));
        }
        id++;
      }
    }
  }
  for (int t = 0; t < (1 << n); t++) {
    put[t] = -1;
    for (int i = n - 1; i >= 0; i--) {
      if (t & (1 << i)) {
        continue;
      }
      put[t] = t ^ (1 << i);
      break;
    }
  }
  for (int t = 0; t < (1 << n); t++) {
    f[t] = 0;
  }
  f[0] = 1;
  for (int i = 0; i < m; i++) {
    for (int t = 0; t < (1 << n) - 1; t++) {
      add(f[put[t]], f[t]);
    }
    for (int id = 0; id < n; id++) {
      for (int t = (1 << n) - 1; t >= 0; t--) {
        if (go[t][id] != -1) {
          add(f[go[t][id]], f[t]);
        }
      }
    }
    for (int t = 0; t < (1 << n); t++) {
      if ((t & need1[i]) != need2[i]) {
        f[t] = 0;
      }
    }
  }
  int ans = 0;
  for (int t = 0; t < (1 << n); t++) {
    add(ans, f[t]);
  }
  printf("%d\n", ans);
  return 0;
}
