import sys input=sys.stdin.readline write=sys.stdout.write #============================入力 H,W=map(int,input().split()) A=[[1]*(W+1) for _ in range(H+1)] for i in range(1,H+1): A[i][1:]=list(map(int,input().split())) #============================前計算 Mod=10**9+7 B=[[1]*(W+1) for _ in range(H+1)] All_prod=1 X_prod=[1]*(H+1) Y_prod=[1]*(W+1) Zero_X=[0]*(H+1) Zero_Y=[0]*(W+1) for x in range(H+1): E=A[x] F=B[x] for y in range(W+1): t=E[y] if t==0: Zero_X[x]+=1 Zero_Y[y]+=1 F[y]=t=1 else: F[y]=t All_prod*=t All_prod%=Mod X_prod[x]*=t X_prod[x]%=Mod Y_prod[y]*=t Y_prod[y]%=Mod Zero_Total=sum(Zero_X) #============================クエリ処理 Q=int(input()) T=[0]*Q for k in range(Q): r,c=map(int,input().split()) zero=Zero_Total-(Zero_X[r]+Zero_Y[c]) if A[r][c]==0: zero+=1 if zero>0: continue T[k]=All_prod*pow(X_prod[r]*Y_prod[c],Mod-2,Mod)*B[r][c] T[k]%=Mod write("\n".join(map(str,T)))