mod=10**9+7 h,w=map(int,input().split()) a=[list(map(int,input().split()))for _ in range(h)] rr=[1]*h cc=[1]*w zrr=[0]*h zcc=[0]*w z=0 ans=1 for i in range(h): for j in range(w): if a[i][j]==0: zrr[i]+=1 zcc[j]+=1 z+=1 continue ans*=a[i][j] ans%=mod rr[i]*=a[i][j] rr[i]%=mod cc[j]*=a[i][j] cc[j]%=mod for _ in range(int(input())): r,c=map(int,input().split()) if z-zrr[r-1]-zcc[c-1]+(a[r-1][c-1]==0):print(0) else:print((ans*pow(rr[r-1],mod-2,mod)*pow(cc[c-1],mod-2,mod)*a[r-1][c-1])%mod)