use std::cmp::*; use std::io::*; const MOD: u64 = 1_000_000_007; fn main() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.trim().split_whitespace(); let h: usize = itr.next().unwrap().parse().unwrap(); let w: usize = itr.next().unwrap().parse().unwrap(); let mut a = vec![vec![0u64; w]; h]; let mut row = vec![0; h]; let mut col = vec![0; w]; let mut ans = 1u64; for i in 0..h { for j in 0..w { a[i][j] = itr.next().unwrap().parse().unwrap(); ans = ans * a[i][j]; } } for i in 0..h { let mut tmp = 1; for j in 0..w { tmp = tmp * a[i][j]; } row[i] = tmp; } for j in 0..w { let mut tmp = 1; for i in 0..h { tmp = tmp * a[i][j]; } col[j] = tmp; } let mut out = Vec::new(); let q: usize = itr.next().unwrap().parse().unwrap(); for _ in 0..q { let r: usize = itr.next().unwrap().parse::().unwrap() - 1; let c: usize = itr.next().unwrap().parse::().unwrap() - 1; let now = ans / (row[r] / a[r][c]) / col[c] % MOD; writeln!(out, "{}", now).ok(); } stdout().write_all(&out).unwrap(); }