#include using namespace std; #define rep(i,a,b) for(int i=a;i= mod ? x - mod : x; } int mul(int x, int y) { return 1LL * x * y % mod; } int modpow(int a, long long b) { int ret = 1; while (b > 0) { if (b & 1) ret = 1LL * ret * a % mod; a = 1LL * a * a % mod; b >>= 1; } return ret; } int modinv(int a) { return modpow(a, mod - 2); } int fac[201010], ifac[201010]; void initfac() { fac[0] = ifac[0] = 1; rep(i, 1, 201010) fac[i] = 1LL * i * fac[i - 1] % mod; rep(i, 1, 201010) ifac[i] = modinv(fac[i]); } //----------------------------------------------------------------------------------- int GX, GY, K; int X[10], Y[10], N[10]; //----------------------------------------------------------------------------------- int ans = 0; int use[10]; void rec(int idx) { if (idx == K) { int x = 0, y = 0; rep(i, 0, K) x += X[i] * use[i]; rep(i, 0, K) y += Y[i] * use[i]; if (x != GX) return; if (y != GY) return; int sm = 0; rep(i, 0, K) sm += use[i]; int com = fac[sm]; rep(i, 0, K) if(use[i] != 0) com = mul(com, modinv(use[i])); ans = add(ans, com); return; } rep(i, 0, N[idx] + 1) { use[idx] = i; rec(idx + 1); } } //----------------------------------------------------------------------------------- int main() { initfac(); cin >> GX >> GY >> K; rep(i, 0, K) cin >> X[i] >> Y[i] >> N[i]; rec(0); cout << ans << endl; }