#include #include using namespace std; using namespace atcoder; typedef long long ll; typedef pair P; typedef modint1000000007 mint; #define rep(i,a,b) for(ll i=a;i=b;i--) ll h,w; ll q; ll r,c; mint s=1; int main(void){ cin.tie(0); ios::sync_with_stdio(0); cin>>h>>w; ll a[h][w]; mint x[h],y[w]; rep(i,0,h){ rep(j,0,w){ cin>>a[i][j]; s*=a[i][j]; } } rep(i,0,h){ x[i]=1; rep(j,0,w){ x[i]*=a[i][j]; } } rep(i,0,w){ y[i]=1; rep(j,0,h){ y[i]*=a[j][i]; } } cin>>q; while(q--){ cin>>r>>c; mint ans=s/x[r-1]/y[c-1]*a[r-1][c-1]; cout<