use std::collections::HashMap; const MOD: usize = 1e9 as usize + 7; fn fact_inv(size: usize) -> (Vec, Vec) { let mut facts = vec![0usize; size+1]; let mut fact_invs = vec![0usize; size+1]; let mut invs = vec![0usize; size+1]; facts[0] = 1; facts[1] = 1; fact_invs[0] = 1; fact_invs[1] = 1; invs[1] = 1; for i in 2..=size { facts[i] = facts[i-1] * i % MOD; invs[i] = MOD - invs[MOD % i] * (MOD / i) % MOD; fact_invs[i] = fact_invs[i - 1] * invs[i] % MOD; } (facts, fact_invs) } fn comb(n: usize, r: usize, facts: &Vec, fact_invs: &Vec) -> usize { facts[n] * fact_invs[r] % MOD * fact_invs[n-r] % MOD } fn main() { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let gx = temp[0]; let gy = temp[1]; let k = temp[2] as usize; let queries = (0..k).map(|_| { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); (temp[0], temp[1], temp[2] as usize) }) .collect::>(); let mut mapping: HashMap<(isize, isize), Vec>> = HashMap::new(); mapping.insert((0isize, 0isize), vec![vec![]]); for idx in 0..k { let (ax, ay, n) = queries[idx]; let mut next_map = HashMap::new(); for (&(x, y), v) in mapping.iter() { for vv in v.iter() { for i in 0..=n { let mut nvv = vv.clone().to_owned(); nvv.push(i); let np = (x + i as isize * ax, y + i as isize * ay); next_map.entry(np).or_insert(vec![]).push(nvv); } } } mapping = next_map; } let (facts, fact_invs) = fact_inv(100); let mut result = 0usize; if let Some(v) = mapping.get(&(gx, gy)) { for vv in v.iter() { let mut summary = vv.iter().sum::(); let mut pat = 1usize; for &val in vv.iter() { pat *= comb(summary, val, &facts, &fact_invs); pat %= MOD; summary -= val; } result += pat; result %= MOD; } } println!("{}", result); }