use std::io::{BufRead, Write}; const MOD: usize = 1_000_000_007; fn main () { let stdin = std::io::stdin(); let stdout = std::io::stdout(); let mut reader = std::io::BufReader::new(stdin.lock()); let mut writer = std::io::BufWriter::new(stdout.lock()); let (h, w): (usize, usize) = { let mut buf = String::new(); reader.read_line(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); ( iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), ) }; let mut a: Vec> = Vec::with_capacity(h); for _ in 0..h { let b: Vec = { let mut buf = String::new(); reader.read_line(&mut buf).unwrap(); let iter = buf.split_whitespace(); iter.map(|x| x.parse().unwrap()).collect() }; a.push(b); } let pow = |mut a: usize, mut n: usize| -> usize { let mut res: usize = 1; while n > 0 { if (n & 1) > 0 { res = res * a % MOD; } a = a * a % MOD; n >>= 1; } res }; let mut res = 1; let mut row: Vec = Vec::with_capacity(h); let mut rzc: usize = 0; for i in 0..h { let mut tmp = 1; for j in 0..w { tmp = tmp * a[i][j] % MOD; } row.push(tmp); if tmp == 0 { rzc += 1; } else { res = res * tmp % MOD; } } let mut column: Vec = Vec::with_capacity(w); let mut czc: usize = 0; for j in 0..w { let mut tmp = 1; for i in 0..h { tmp = tmp * a[i][j] % MOD; } column.push(tmp); if tmp == 0 { czc += 1; } } let q: usize = { let mut buf = String::new(); reader.read_line(&mut buf).unwrap(); buf.trim_end().parse().unwrap() }; for _ in 0..q { let (r, c): (usize, usize) = { let mut buf = String::new(); reader.read_line(&mut buf).unwrap(); let mut iter = buf.split_whitespace(); ( iter.next().unwrap().parse::().unwrap() - 1, iter.next().unwrap().parse::().unwrap() - 1, ) }; if rzc > 1 || czc > 1 { writeln!(writer, "0").unwrap(); } else { let mut ans = res; ans = { if rzc > 0 { if row[r] > 0 { 0 } else { ans } } else { ans * pow(row[r], MOD - 2) % MOD } }; ans = { if czc > 0 { if column[r] > 0 { 0 } else { ans } } else { ans * pow(column[c], MOD - 2) % MOD } }; if a[r][c] > 0 { ans = ans * a[r][c] % MOD; } writeln!(writer, "{}", ans).unwrap(); } } }