def solve(): mod = 10**9+7 H, W = map(int, input().split()) h_prod = [1]*H w_prod = [1]*W A = [list(map(int, input().split())) for _ in range(H)] for i in range(H): for j in range(W): h_prod[i] *= A[i][j] w_prod[j] *= A[i][j] h_prod[i] %= mod w_prod[j] %= mod Q = int(input()) total = 1 for i in range(H): total *= h_prod[i] total %= mod ans = [total]*Q for i in range(Q): r,c = map(int, input().split()) ans[i] *= pow(h_prod[r-1]*w_prod[c-1],mod-2,mod)*A[r-1][c-1] ans[i] %= mod return ans print(*solve(),sep='\n')