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 ans=1 for i in range(h): for j in range(w): 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()) print((ans*pow(rr[r-1],mod-2,mod)*pow(cc[c-1],mod-2,mod)*a[r-1][c-1])%mod)