const MOD: usize = 1e9 as usize + 7; fn power(base: usize, times: usize) -> usize { if times == 0 { return 1usize; } if times == 1 { return base; } let temp = power(base, times/2); temp * temp % MOD * power(base, times%2) % MOD } fn main() { let mut hw = String::new(); std::io::stdin().read_line(&mut hw).ok(); let hw: Vec = hw.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let h = hw[0]; let w = hw[1]; let grid = (0..h).map(|_| { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); temp }) .collect::>(); let mut q = String::new(); std::io::stdin().read_line(&mut q).ok(); let q: usize = q.trim().parse().unwrap(); let queries = (0..q).map(|_| { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); (temp[0]-1, temp[1]-1) }) .collect::>(); let row_zero_cnt = (0..h).map(|i| (0..w).filter(|&j| grid[i][j] == 0).count()).collect::>(); let col_zero_cnt = (0..w).map(|j| (0..h).filter(|&i| grid[i][j] == 0).count()).collect::>(); let total_zero_cnt = row_zero_cnt.iter().sum::(); let row_prod = (0..h).map(|i| (0..w).filter(|&j| grid[i][j] > 0).fold(1usize, |x, y| x * grid[i][y] % MOD)).collect::>(); let col_prod = (0..w).map(|j| (0..h).filter(|&i| grid[i][j] > 0).fold(1usize, |x, y| x * grid[y][j] % MOD)).collect::>(); let total_prod = row_prod.iter().fold(1usize, |x, &y| x * y % MOD); for &(r, c) in queries.iter() { let zero_cnt = row_zero_cnt[r] + col_zero_cnt[c] - if grid[r][c] == 0 { 1 } else { 0 }; if zero_cnt < total_zero_cnt { println!("0"); continue; } let modval = power(row_prod[r], MOD-2) * power(col_prod[c], MOD-2) % MOD * if grid[r][c] == 0 { 1 } else { grid[r][c] } % MOD; println!("{}", total_prod * modval % MOD); } }