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