/*** 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; ll mul = 1; rep(i,0,H){ rep(j,0,W){ 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; } 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(); }