#include int mod = 1000000007; #define ll long using namespace std; int dp[400], dp2[400]; int at(int a, int b){ return dp2[20*a+b]; } int issempty(int a){ if (a == -1) return 1; return 0; } int op(int s){ if(s <= 9) { return s + 1; } return 19 - s; } int main() { int n,k,m; scanf("%d %d %d", &n, &k, &m); short vyplnene[2*n]; for(int i = 0; i < 2*n; i++) { vyplnene[i] = -1; } for(int i = 0; i < m; i++) { int x, y, v; scanf("%d %d %d", &x, &y, &v); vyplnene[x*2+y] = v; } int fixed = issempty(vyplnene[0]) + issempty(vyplnene[1]); int fixed2 = issempty(vyplnene[2])+ issempty(vyplnene[3]); //cout<<"fixed"<k) continue; dp[20*f+s] = op(f)*op(s); } } } else if(fixed == 1 && fixed2 == 2){ int t = vyplnene[0]+vyplnene[1]+1; for(int f = t; f < 10+t; f++) { for(int s = 0; s < 19; s++) { if(f+s>k) continue; dp[20*f+s] = op(s); } } } else if(fixed == 2 && fixed2 == 1){ int t = vyplnene[2]+vyplnene[3]+1; for(int f = 0; f < 19; f++) { for(int s = t; s < 10+t; s++) { if(f+s>k) continue; dp[20*f+s] = op(f); } } } else if(fixed == 1 && fixed2 == 1){ int t1 = vyplnene[0]+vyplnene[1]+1; int t2 = vyplnene[2]+vyplnene[3]+1; for(int f = t1; f < 10+t1; f++) { for(int s = t2; s < 10+t2; s++) { if(f+s>k) continue; dp[20*f+s] = 1; } } } else if(fixed == 0 && fixed2 == 1){ int f = vyplnene[0]+vyplnene[1]; int t2 = vyplnene[2]+vyplnene[3]+1; for(int s = t2; s < 10+t2; s++) { if(f+s>k) continue; dp[20*f+s] = 1; } } else if(fixed == 1 && fixed2 == 0){ int t1 = vyplnene[0]+vyplnene[1]+1; int s = vyplnene[2]+vyplnene[3]; for(int f = t1; f < 10+t1; f++) { if(f+s>k) continue; dp[20*f+s] = 1; } } else{ int f = vyplnene[0]+vyplnene[1]; int s = vyplnene[2]+vyplnene[3]; if(f+s<=k){ dp[20*f+s] = 1; } } //cout<< "3,5"<< dp[20*3+5]<= 19) continue; if(k-f-s < 0) continue; dp[20*s+k-f-s] = (at(f,s)*op(k-f-s) + dp[20*s+k-f-s]) % mod; } } } else if(vyplnene[2*i] * vyplnene[2*i+1] < 0 && 0 != vyplnene[2*i] + vyplnene[2*i+1]) { int t = vyplnene[2*i] + vyplnene[2*i+1] + 1; for(int f = 0; f < 20; f++) { for(int s = 0; s < 20; s++) { if(k-f-s-t < 0) continue; if(k-f-s-t >= 10) continue; dp[20*s+k-f-s] = (at(f,s) + dp[20*s+k-f-s]) % mod; } } } else { int t = vyplnene[2*i] + vyplnene[2*i+1]; for(int f = 0; f < 20; f++) { int s = k-f-t; if(s < 0) continue; if(s >= 19) continue; dp[20*s+k-f-s] = (at(f,s) + dp[20*s+k-f-s]) % mod; } } } int res = 0; for(int i = 0; i < 400; i++) { res += dp[i]; //cout<<"dp[]"<