#include #define ft first #define sc second #define pt(sth) cout << sth << "\n" using namespace std; typedef long long ll; typedef pair pll; templatebool chmax(T &a, const T &b) {if(abool chmin(T &a, const T &b) {if(b> a[i]; ll mop(ll x, ll n) { ll res=1; while(n>0) { if(n&1) (res*=x)%=MOD; (x*=x)%=MOD; n>>=1; } return res; } int main(void) { ll i, j, k; ll H, W; cin >> H >> W; ll a[H+10][W+10]; ll zh[MAX]={}, zw[MAX]={}; ll mh[MAX], mw[MAX]; ll M=1; ll Z=0; for(i=0; i> a[i][j]; if(a[i][j]==0) { Z++; zh[i]++; zw[j]++; }else { (M*=a[i][j])%=MOD; (mh[i]*=a[i][j])%=MOD; (mw[j]*=a[i][j])%=MOD; } } } ll Q; cin >> Q; for(ll _=0; _> r >> c; r--; c--; ll z=zh[r]+zw[c]-(a[r][c]==0); if(z==Z) { pt(M*a[r][c]%MOD*mop(mh[r], MOD-2)%MOD*mop(mw[c], MOD-2)%MOD); }else { pt(0); } } }