#include using namespace std; typedef long long int64; const int mod = 1e9 + 7; inline int64 modPow(int64 x, int64 n) { if(n == 0) return (1); int64 ret = modPow(x, n / 2); (ret *= ret) %= mod; if(n & 1) (ret *= x) %= mod; return (ret); } inline int64 modInv(int64 a) { return (modPow(a, mod - 2)); } inline int64 modCombination(int p, int q) { static int64 fact[202020], rfact[202020]; if(fact[0] == 0) { fact[0] = rfact[0] = 1; for(int i = 1; i < 102020; i++) { fact[i] = fact[i - 1] * i % mod; rfact[i] = modInv(fact[i]); } } if(q < 0 || p < q) return (0); return (fact[p] * rfact[q] % mod * rfact[p - q] % mod); } int main() { int Gx, Gy, K; int x[5], y[5], N[5] = {}; cin >> Gx >> Gy >> K; for(int i = 0; i < K; i++) { cin >> x[i] >> y[i] >> N[i]; } vector< pair< int, int > > v[5]; for(int i = 0; i < 5; i++) { v[i].emplace_back(0, 0); } for(int i = 0; i < K; i++) { int xx = 0, yy = 0; for(int j = 0; j < N[i]; j++) { xx += x[i], yy += y[i]; v[i].emplace_back(xx, yy); } } int ret = 0; for(int i = 0; i <= N[0]; i++) { for(int j = 0; j <= N[1]; j++) { for(int k = 0; k <= N[2]; k++) { for(int l = 0; l <= N[3]; l++) { for(int m = 0; m <= N[4]; m++) { int all = i + j + k + l + m; int xx = v[0][i].first + v[1][j].first + v[2][k].first + v[3][l].first + v[4][m].first; int yy = v[0][i].second + v[1][j].second + v[2][k].second + v[3][l].second + v[4][m].second; if(xx != Gx || yy != Gy) continue; int64 aa = modCombination(all, i); int64 bb = modCombination(all - i, j); int64 cc = modCombination(all - i - j, k); int64 dd = modCombination(all - i - j - k, l); int64 ee = modCombination(all - i - j - k - l, m); ret += aa * bb % mod * cc % mod * dd % mod * ee % mod; ret %= mod; } } } } } cout << ret << endl; }