class Integer def mod_inverse(mod) self.pow(mod - 2, mod) end end H, W = gets.split.map(&:to_i) A = H.times.map { gets.split.map(&:to_i) } MOD = 10 ** 9 + 7 R = [] zero_cnt = 0 ZR = Array.new(H, 0) ZC = Array.new(W, 0) all = 1 H.times do |i| W.times do |j| all *= [1, A[i][j]].max all %= MOD if A[i][j] == 0 ZR[i] += 1 ZC[j] += 1 zero_cnt += 1 end end end H.times do |i| row = 1 W.times do |j| row *= [1, A[i][j]].max row %= MOD end R << row end C = [] W.times do |j| col = 1 H.times do |i| col *= [1, A[i][j]].max col %= MOD end C << col end Q = gets.to_i Q.times do r, c = gets.split.map(&:to_i) ans = all ans *= R[r - 1].mod_inverse(MOD) * C[c - 1].mod_inverse(MOD) * [1, A[r - 1][c - 1]].max zc = zero_cnt - ZR[r - 1] - ZC[c - 1] zc += 1 if A[r - 1][c - 1] == 0 if zc > 0 puts 0 else puts ans % MOD end end