#include using namespace std; #define FOR(a, b, c) for(int a = b; a < c; ++a) using ll = long long; const int MOD = 1e9 + 7; const int N = 1e5 + 7; int tab[N][2]; int ile[20]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k, m; cin >> n >> k >> m; FOR(i, 0, 10) FOR(j, 0, 10) ile[i + j]++; FOR(i, 0, n) FOR(j, 0, 2) tab[i][j] = -1; while(m--) { int c, r; cin >> c >> r; cin >> tab[c][r]; } ll res = 0; FOR(s0, 0, 19) { FOR(s1, 0, 19) { FOR(s2, 0, 19) { if(s0 + s1 + s2 != k) { continue; } ll temp = 1; vector s = {s0, s1, s2}; FOR(i, 0, n) { int id = i % 3; if(tab[i][0] != -1 and tab[i][1] != -1) { if(tab[i][0] + tab[i][1] != s[id]) { temp = 0; break; } } else if(tab[i][0] != -1 or tab[i][1] != -1) { int val = tab[i][0]; if(tab[i][0] == -1) val = tab[i][1]; if(s[id] < val or (s[id] - val > 9)) { temp = 0; break; } } else { temp *= ile[s[id]]; temp %= MOD; } } res += temp; res %= MOD; } } } cout << res << "\n"; }