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); for i in 0..h { let mut tmp = 1; for j in 0..w { tmp = tmp * a[i][j] % MOD; res = res * a[i][j] % MOD; } row.push(tmp); } let mut column: Vec = Vec::with_capacity(w); for j in 0..w { let mut tmp = 1; for i in 0..h { tmp = tmp * a[i][j] % MOD; } column.push(tmp); } 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, ) }; let mut ans = res; ans = ans * pow(row[r], MOD - 2) % MOD; ans = ans * pow(column[c], MOD - 2) % MOD; ans = ans * a[r][c] % MOD; writeln!(writer, "{}", ans).unwrap(); } }