#include using namespace std; #define EPS (1e-10) #define REP (i,n) for (int i = 0; i<(n);i++) typedef long double ld; typedef long long ll; typedef pair ii; typedef pair dd; typedef vector vi; typedef vector vll; typedef vector vd; ll pw(ll a, ll p, ll M) { ll res = 1; while (p > 0) { if (p&1) res = (res*a) %M; a = (a*a)%M; p /= 2; } return res; } int main() { ios::sync_with_stdio(false); int N, K, M; cin >> N >> K >> M; vector a(N, vi(2, -1)); for(int i = 0; i < M; ++i) { int c, r, v; cin >> c >> r >> v; a[c][r] = v; } int m1 = 0, M1 = 18, m2 = 0, M2 = 18, m3 = 0, M3 = 18; int c1=0,c2=0,c3 = 0; bool unsolv = false; for(int i = 0; i < N; i += 3) { if (a[i][0] == -1 && a[i][1] == -1) { c1++; } else if (a[i][0] != -1 && a[i][1] != -1) { int s = a[i][0] + a[i][1]; if (m1 > s || M1 < s) { unsolv = true; break; } else { m1 = M1 = s; } } else { int s = a[i][0] + a[i][1] + 1; m1 = max(s, m1); M1 = min(9+m1, M1); } } for(int i = 1; i < N; i += 3) { if (a[i][0] == -1 && a[i][1] == -1) { c2++; } else if (a[i][0] != -1 && a[i][1] != -1) { int s = a[i][0] + a[i][1]; if (m2 > s || M2 < s) { unsolv = true; break; } else { m2 = M2 = s; } } else { int s = a[i][0] + a[i][1] + 1; m2 = max(s, m2); M2 = min(M2, 9+m2); } } for(int i = 2; i < N; i += 3) { if (a[i][0] == -1 && a[i][1] == -1) { c3++; } else if (a[i][0] != -1 && a[i][1] != -1) { int s = a[i][0] + a[i][1]; if (m3 > s || M3 < s) { unsolv = true; break; } else { m3 = M3 = s; } } else { int s = a[i][0] + a[i][1] + 1; m3 = max(s, m3); M3 = min(M3, 9+m3); }// for(int i = 0; i < N) } if (unsolv) cout << 0 << endl; else { // cout << c1 << " " << c2 << " " << c3 << endl; // cout << m1 << " " << M1 << endl; // cout << m2 << " " << M2 << endl; // cout << m3 << " " << M3 << endl; long long MOD = 1000000007; long long res = 0; for(int k1 = m1; k1 <= M1; ++k1) { for(int k2 = m2; k2 <= M2; ++k2) { for(int k3 = m3; k3 <= M3; ++k3) { if (k1 +k2 +k3 == K) { ll kk1 = k1 <= 9 ? k1+1 : 19-k1; ll kk2 = k2 <= 9 ? k2+1 : 19-k2; ll kk3 = k3 <= 9 ? k3+1 : 19-k3; // cout << k1 << " " << k2 << " " << k3 << endl; ll xx = pw(kk1, c1, MOD); xx *= pw(kk2, c2, MOD); xx %= MOD; xx *= pw(kk3, c3, MOD); xx %= MOD; res += xx; res %= MOD; // ll aa = k1-5, bb=k2-2; // cout << (10-aa-bb+1)*(2+bb+1)*(5+aa+1) << endl; // cout << xx << endl; // cout << res << endl; } } } } cout << res << endl; } }