import sys S=sys.stdin.readlines() H,W=map(int,S[0].split()) A=[list(map(int,S[i+1].split())) for i in range(H)] mod=10**9+7 M1=[1]*H C1=[0]*H M2=[1]*W C2=[0]*W MA=1 CA=0 for i in range(H): for j in range(W): if A[i][j]: M1[i]=M1[i]*A[i][j]%mod M2[j]=M2[j]*A[i][j]%mod MA=MA*A[i][j]%mod else: C1[i]+=1 C2[j]+=1 CA+=1 Q=int(S[H+1]) r,c=0,0 for i in range(Q): r,c=map(int,S[H+2+i].split()) r,c=r-1,c-1 if CA-C1[r]-C2[c]+(A[r][c]==0): print(0) else: if A[r][c]: print(MA*pow(M1[r]*M2[c],mod-2,mod)*A[r][c]%mod) else: print(MA*pow(M1[r]*M2[c],mod-2,mod)%mod)