const MOD: u64 = 1_000000000 + 7; #[derive(Debug)] struct Solver { gx: i64, gy: i64, x: Vec, y: Vec, c: Vec, comb: Vec>, used: Vec, } impl Solver { fn new() -> Self { let mut scanner = Scanner::new(); let gx = scanner.next_i64(); let gy = scanner.next_i64(); let n = scanner.next_usize(); let mut x = Vec::new(); let mut y = Vec::new(); let mut c = Vec::new(); for _ in 0..n { x.push(scanner.next_i64()); y.push(scanner.next_i64()); c.push(scanner.next_usize()); } let sum: usize = c.iter().sum(); let mut comb = vec![vec![0; sum+1]; sum+1]; for i in 0..sum+1 { comb[i][0] = 1; comb[i][i] = 1; for j in 1..i { comb[i][j] = comb[i-1][j-1] + comb[i-1][j]; if comb[i][j] >= MOD { comb[i][j] -= MOD } } } Solver { gx: gx, gy: gy, x: x, y: y, c: c, comb: comb, used: vec![0; n] } } fn calc(&mut self) -> u64 { let mut sum: usize = self.used.iter().sum(); let mut x = 0; let mut y = 0; let mut ans = 1_u64; for i in 0..self.used.len() { ans = ans * self.comb[sum][self.used[i]] % MOD; x += self.x[i] * self.used[i] as i64; y += self.y[i] * self.used[i] as i64; sum -= self.used[i]; } if x == self.gx && y == self.gy { ans } else { 0 } } fn rec(&mut self, idx: usize) -> u64 { if idx >= self.used.len() { return self.calc(); } let mut ans = 0; for i in 0..self.c[idx]+1 { self.used[idx] = i; ans += self.rec(idx + 1); } ans % MOD } } fn main() { let mut solver = Solver::new(); println!("{}", solver.rec(0)); } struct Scanner { reader: std::io::Stdin, tokens: std::collections::VecDeque, } impl Scanner { fn new() -> Self { Scanner { reader: std::io::stdin(), tokens: std::collections::VecDeque::new(), } } fn next(&mut self) -> String { if self.tokens.is_empty() { let mut s = String::new(); loop { self.reader.read_line(&mut s).ok(); let s = s.trim(); if s.len() != 0 { for it in s.split_whitespace() { self.tokens.push_back(it.into()) } break; } } } self.tokens.pop_front().unwrap() } fn next_generics(&mut self) -> T where T: std::str::FromStr + std::marker::Copy, ::Err: std::fmt::Debug { self.next().parse().unwrap() } #[allow(dead_code)] fn next_i32(&mut self) -> i32 { self.next_generics::() } #[allow(dead_code)] fn next_i64(&mut self) -> i64 { self.next_generics::() } #[allow(dead_code)] fn next_u64(&mut self) -> u64 { self.next_generics::() } #[allow(dead_code)] fn next_usize(&mut self) -> usize { self.next_generics::() } }