from collections import defaultdict from collections import Counter 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)] h_zero,w_zero = [],[] for i in range(H): for j in range(W): if A[i][j]==0: h_zero.append(i) w_zero.append(j) continue h_prod[i] *= A[i][j] w_prod[j] *= A[i][j] h_prod[i] %= mod w_prod[j] %= mod h_zero,w_zero = list(set(h_zero)),list(set(w_zero)) hl,wl = len(h_zero),len(w_zero) 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()) if hl>1 and wl>1: ans[i] = 0 continue if hl==1 and wl>1 and r-1!=h_zero[0]: ans[i] = 0 continue if hl>1 and wl==1 and c-1!=w_zero[0]: ans[i] = 0 continue if hl==1 and wl==1 and r-1!=h_zero[0] and c-1!=w_zero[0]: ans[i] = 0 continue ans[i] *= pow(h_prod[r-1]*w_prod[c-1],mod-2,mod) if A[r-1][c-1]!=0: ans[i] *= A[r-1][c-1] ans[i] %= mod return ans print(*solve(),sep='\n')