from functools import reduce MOD = 10 ** 9 + 7 def mulmod(a, b): return a * b % MOD def prodmod(iterable): return reduce(mulmod, list(iterable)) H, W = map(int, input().split()) As = [list(map(int, input().split())) for _ in range(H)] zeros = [[1 if elem == 0 else 0 for elem in row] for row in As] As = [[1 if elem == 0 else elem for elem in row] for row in As] zero_r_sum = [sum(row) for row in zeros] zero_c_sum = [sum(zeros[i][j] for i in range(H)) for j in range(W)] zero_sum = sum(zero_r_sum) As_r_prod = [prodmod(row) for row in As] As_c_prod = [prodmod(As[i][j] for i in range(H)) for j in range(W)] As_prod = prodmod(As_r_prod) Q = int(input()) for _ in range(Q): r, c = map(int, input().split()) r -= 1 c -= 1 zero_count = zero_sum - zero_r_sum[r] - zero_c_sum[c] + zeros[r][c] if zero_count >= 1: print(0) else: print(mulmod(mulmod(As_prod, As[r][c]), pow(mulmod(As_r_prod[r], As_c_prod[c]), MOD - 2, MOD)))