import sys input = sys.stdin.readline mod = 10**9 + 7 class Accumulate2D: def __init__(self, A): self.H = len(A) self.W = len(A[0]) self.Ac = [[1] * (self.W + 1) for _ in range(self.H + 1)] for i in range(1, self.H + 1): now = 1 for j in range(1, self.W + 1): now *= A[i-1][j-1] now %= mod self.Ac[i][j] = self.Ac[i - 1][j] * now self.Ac[i][j] %= mod def calc_prod(self, C, D): return self.Ac[C][D] def rot90(A): # 時計回りに90度回転 return list(zip(*(A[::-1]))) H, W = map(int, input().split()) A = [] for i in range(H): A.append(list(map(int, input().split()))) G1 = Accumulate2D(A) A = rot90(A) G2 = Accumulate2D(A) A = rot90(A) G3 = Accumulate2D(A) A = rot90(A) G4 = Accumulate2D(A) Q = int(input()) for _ in range(Q): h, w = map(int, input().split()) h, w = h - 1, w - 1 ans = G1.calc_prod(h, w)*G2.calc_prod(w, H-h-1)*G3.calc_prod(H-h-1,W-w-1)*G4.calc_prod(W-w-1,h) print(ans%mod)