use std::io::*; const MOD: u64 = 1_000_000_007; fn mod_pow(mut x: u64, mut e: u64, m: u64) -> u64 { let mut res = 1; while e > 0 { if e & 1 == 1 { res = res * x % m; } x = x * x % m; e >>= 1; } res } 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; // 0 kazoeru let mut all0 = 0; let mut row0 = vec![0; h]; let mut col0 = vec![0; w]; for i in 0..h { for j in 0..w { a[i][j] = itr.next().unwrap().parse().unwrap(); if a[i][j] == 0 { all0 += 1; } else { ans = ans * a[i][j] % MOD; } } } for i in 0..h { let mut tmp = 1; for j in 0..w { if a[i][j] == 0 { row0[i] += 1; } else { tmp = tmp * a[i][j] % MOD; } } row[i] = tmp; } for j in 0..w { let mut tmp = 1; for i in 0..h { if a[i][j] == 0 { col0[j] += 1; } else { tmp = tmp * a[i][j] % MOD; } } 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 zero = all0 - row0[r] - col0[c] + (a[r][c] == 0) as i32; if zero == 0 { let mut now = ans * mod_pow(row[r], MOD - 2, MOD) % MOD * mod_pow(col[c], MOD - 2, MOD) % MOD; if a[r][c] != 0 { now = now * a[r][c] % MOD; } writeln!(out, "{}", now).ok(); } else { writeln!(out, "0").ok(); } } stdout().write_all(&out).unwrap(); }