#include #include #include #include #define repeat(i,n) for (int i = 0; (i) < int(n); ++(i)) #define repeat_from(i,m,n) for (int i = (m); (i) < int(n); ++(i)) using ll = long long; using namespace std; constexpr int mod = 1e9+7; ll powmod(ll x, ll y) { // O(log y) assert (0 <= x and x < mod); assert (0 <= y); ll z = 1; for (ll i = 1; i <= y; i <<= 1) { if (y & i) z = z * x % mod; x = x * x % mod; } return z; } ll inv(ll x) { // p must be a prime, O(log p) return powmod(x, mod-2); } int fact(int n) { static vector memo(1,1); if (memo.size() <= n) { int l = memo.size(); memo.resize(n+1); repeat_from (i,l,n+1) memo[i] = memo[i-1] *(ll) i % mod; } return memo[n]; } int choose(int n, int r) { // O(n) at first time, otherwise O(\log n) if (n < r) return 0; r = min(r, n - r); return fact(n) *(ll) inv(fact(n-r)) % mod *(ll) inv(fact(r)) % mod; } int main() { int w, h, k; scanf("%d%d%d", &w, &h, &k); assert (k <= 5); array x = {}; array y = {}; array n = {}; repeat (i,k) scanf("%d%d%d", &x[i], &y[i], &n[i]); ll acc = 0; array i; for (i[4] = 0; i[4] <= n[4]; ++ i[4]) for (i[3] = 0; i[3] <= n[3]; ++ i[3]) for (i[2] = 0; i[2] <= n[2]; ++ i[2]) for (i[1] = 0; i[1] <= n[1]; ++ i[1]) for (i[0] = 0; i[0] <= n[0]; ++ i[0]) { ll x_acc = 0; ll y_acc = 0; repeat (j,k) { x_acc += i[j] *(ll) x[j]; y_acc += i[j] *(ll) y[j]; } if (x_acc == w and y_acc == h) { ll cnt = 1; int i_acc = 0; repeat (j,k) { i_acc += i[j]; cnt = cnt * choose(i_acc, i[j]) % mod; } acc += cnt; } } printf("%lld\n", acc); return 0; }