#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define REP(i, n) for (int (i) = 0; (i) < (n); (i)++) #define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++) #define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--) #define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--) #define DEBUG(C) cerr << #C << " = " << C << endl; using LL = long long; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; using VD = vector; using VVD = vector; using PII = pair; using PDD = pair; using PLL = pair; using VPII = vector; template using VT = vector; #define ALL(a) begin((a)), end((a)) #define RALL(a) rbegin((a)), rend((a)) #define SORT(a) sort(ALL((a))) #define RSORT(a) sort(RALL((a))) #define REVERSE(a) reverse(ALL((a))) #define MP make_pair #define FORE(a, b) for (auto &&a : (b)) #define FIND(s, e) ((s).find(e) != (s).end()) #define EB emplace_back templateinline bool chmax(T &a, T b){return (a < b ? a = b, true : false);} templateinline bool chmin(T &a, T b){return (a > b ? a = b, true : false);} const int INF = 1e9; const int MOD = INF + 7; const LL LLINF = 1e18; const long double EPS = 1e-9; const int MAX = 1000; int gx, gy, K, N = 0; LL x[MAX], y[MAX]; int cnt[MAX]; const int MAX_P = 1e5 + 10; template class ModCalc { private: const int defnum = -1; public: ModCalc() { } int add(int a, int b) { a += b; while (a < 0) a += mod; while (a >= mod) a -= mod; return a; } int mul(int a, int b) { return (long long)a * b % mod; } int pow(int a, int p) { int res = 1; while (p > 0) { if (p & 1) res = this->mul(res, a); a = this->mul(a, a); p >>= 1; } return res; } int inverse(int a) { return this->pow(a, mod - 2); } }; ModCalc mc; int memoFact[MAX], memoInvFact[MAX]; int fact(int n) { if (memoFact[n] != -1) return memoFact[n]; return memoFact[n] = (n == 0 ? 1 : mc.mul(n, fact(n - 1))); } int invFact(int n) { if (memoInvFact[n] != -1) return memoInvFact[n]; return memoInvFact[n] = mc.inverse(fact(n)); } LL comb(LL n, LL r) { return mc.mul(mc.mul(fact(n), invFact(r)), invFact(n - r)); } // M個の物をN人で分ける LL combWithRepetition(LL M, LL N) { return comb(M + N - 1, N - 1); } //最初に呼んで♪ void init() { memset(memoFact, -1, sizeof(memoFact)); memset(memoInvFact, -1, sizeof(memoInvFact)); memoFact[0] = 1; for (int i = 0; i < MAX; i++) { fact(i); invFact(i); } } inline LL f(LL x, LL y) { return (LL)x + MAX_P * y; } inline PLL rf(LL num) { return MP(num % MAX_P, num / MAX_P); } int main() { init(); scanf("%d%d%d", &gx, &gy, &K); REP(i, K) { scanf("%lld%lld%d", x + i, y + i, cnt + i); } LL ans = 0; REP(i, cnt[0] + 1) REP(j, cnt[1] + 1) REP(k, cnt[2] + 1) REP(l, cnt[3] + 1) REP(m, cnt[4] + 1) { LL X = x[0] * i + x[1] * j + x[2] * k + x[3] * l + x[4] + m; LL Y = y[0] * i + y[1] * j + y[2] * k + y[3] * l + y[4] + m; if (X == gx && Y == gy) { LL inv = mc.mul(invFact(i), mc.mul(invFact(j), mc.mul(invFact(k), mc.mul(invFact(l), invFact(m))))); (ans += (fact(i + j + k + l + m) * inv)) %= MOD; } } cout << ans << endl; }