H,W = list(map(int, input().split())) A = [] for _ in range(H): A.append(list(map(int, input().split()))) mod = 10**9+7 AA = [[1]*(H+2)] A[0][0]%=mod for i in range(H): a=[1] for j in range(W): temp = a[-1]*A[i][j] #temp%=mod a.append(temp) a.append(1) AA.append(a) for j in range(W+1): for i in range(1,H+1): AA[i][j]*=AA[i-1][j] #AA[i][j]%=mod AA.append([1]*(H+2)) #print(AA) Q = int(input()) for _ in range(Q): r,c=list(map(int, input().split())) #左上 LU = AA[r-1][c-1] #右上 RU = AA[r-1][W]//AA[r-1][c] #左下 LB = AA[H][c-1]//AA[r][c-1] #右下 RB = ((AA[H][W]*AA[r][c])//AA[r][W])//AA[H][c] #print(LU,RU,LB,RB) #print((LU*RU*LB*((AA[H][W]*AA[r][c])//AA[r][W])//AA[H][c])%mod) print(LU*RU*LB*RB%mod)