/*** author: yuji9511 ***/ #include using namespace std; using ll = long long; using lpair = pair; const ll MOD = 1e9+7; const ll INF = 1e18; #define rep(i,m,n) for(ll i=(m);i<(n);i++) #define rrep(i,m,n) for(ll i=(m);i>=(n);i--) #define printa(x,n) for(ll i=0;i void print(H&& h, T&&... t){cout<(t)...);} ll power(ll x, ll n){ if(n == 0) return 1LL; ll res = power(x * x % MOD, n/2); if(n % 2 == 1) res = res * x % MOD; return res; } void solve(){ ll H,W; cin >> H >> W; vector< vector > A(H+1, vector(W+1)); rep(i,0,H){ rep(j,0,W){ cin >> A[i][j]; } } ll Q; cin >> Q; bool zero_tate[100010] = {}, zero_yoko[100010] = {}; rep(i,0,H){ rep(j,0,W){ if(A[i][j] == 0){ zero_tate[i] = true; zero_yoko[j] = true; } } } ll mul = 1; rep(i,0,H){ rep(j,0,W){ if(zero_tate[i] == false && zero_yoko[j] == false){ mul *= A[i][j]; mul %= MOD; } } } ll tate[100010] = {}, yoko[100010] = {}; rep(i,0,H){ ll res = 1; rep(j,0,W){ res *= A[i][j]; res %= MOD; } tate[i] = res; } rep(j,0,W){ ll res = 1; rep(i,0,H){ res *= A[i][j]; res %= MOD; } yoko[j] = res; } ll c1 = 0, c2 = 0; ll p1 = -1, p2 = -1; rep(i,0,H){ if(zero_tate[i]) c1++; } rep(j,0,W){ if(zero_yoko[j]) c2++; } if(c1 >= 2 || c2 >= 2){ while(Q--){ ll r,c; cin >> r >> c; print(0); } }else{ while(Q--){ ll r,c; cin >> r >> c; r--; c--; ll ans = mul; if(c1 > 0 && zero_tate[r] == false){ print(0); }else if(c2 > 0 && zero_yoko[c] == false){ print(0); }else{ if(zero_tate[r] == false){ ans *= power(tate[r], MOD-2); ans %= MOD; if(zero_yoko[c] == false){ ans *= power(yoko[c], MOD-2); ans %= MOD; ans *= A[r][c]; ans %= MOD; print(ans); }else{ print(ans); } }else{ if(zero_yoko[c] == false){ ans *= power(yoko[c], MOD-2); ans %= MOD; print(ans); }else{ print(ans); } } } } } // while(Q--){ // ll r,c; // cin >> r >> c; // r--; c--; // ll ans = mul * power(tate[r],MOD-2) % MOD * power(yoko[c], MOD-2) % MOD * A[r][c] % MOD; // print(ans); // } } int main(){ cin.tie(0); ios::sync_with_stdio(false); solve(); }