#include using namespace std; using ll=long long; using vin=vector; using vll=vector; using vvin=vector>; using vvll=vector>; using vstr=vector; using vvstr=vector>; using vch=vector; using vvch=vector>; using vbo=vector; using vvbo=vector>; using vpii=vector>; using pqsin=priority_queue,greater>; #define mp make_pair #define rep(i,n) for(int i=0;i<(int)(n);i++) #define rep2(i,s,n) for(int i=(s);i<(int)(n);i++) #define all(v) v.begin(),v.end() #define decp(n) cout<>h>>w; vvll a(h,vll(w)); vll row(h,(ll)1),column(w,(ll)1); vin zerorow(h),zerocolumn(w); ll allzero=(ll)0; bool zero=false; ll ap=(ll)1;ll tmp; rep(i,h)rep(j,w){ cin>>a[i][j]; if(a[i][j]==0){ zerorow[i]++; zerocolumn[j]++; allzero++; zero=true; continue; } ap*=a[i][j];ap%=inf; tmp=mondiv(a[i][j]); row[i]*=tmp;row[i]%=inf; column[j]*=tmp;column[j]%=inf; } int q;cin>>q; vll ans;int r,c; rep(i,q){ cin>>r>>c;r--;c--; if(zero){ tmp=zerorow[r]+zerocolumn[c]; if(a[r][c]==0)tmp--; if(tmp!=allzero){ ans.push_back(0); continue; } } tmp=ap*row[r];tmp%=inf; tmp*=column[c];tmp%=inf; tmp*=a[r][c];tmp%=inf; ans.push_back(tmp); } rep(i,ans.size())cout<