#include using namespace std; #define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; ++i) #define rep2(i, x, n) for(ll i = x, i##_len = (n); i < i##_len; ++i) #define all(n) begin(n), end(n) using ll = long long; using P = pair; using vi = vector; using vl = vector; using vs = vector; using vc = vector; using vb = vector; using vd = vector; vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1}; const int mod = 1000000007; struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint& operator+=(const mint a) { if((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if((x += mod - a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(ll t) const { if(!t) return 1; mint a = pow(t >> 1); a *= a; if(t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint& operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream& operator>>(istream& is, const mint& a) { return is >> a.x; } ostream& operator<<(ostream& os, const mint& a) { return os << a.x; } mint fact(int n) { if(n == 1 || n == 0) return 1; mint res = n; return res * fact(n - 1); } int main() { ll h, w; cin >> h >> w; vector r(h, 1), c(w, 1); vector a(h, vl(w)), za(h, vl(w, 0)); vl zr(h, 0), zc(w, 0); ll zero = 0; mint s = 1; rep(i, h) rep(j, w) { ll b; cin >> b; if(b) { a[i][j] = b; r[i] *= b; c[j] *= b; s *= b; } else { za[i][j] = 1; zr[i]++; zc[j]++; zero++; } } ll q; cin >> q; rep(_, q) { ll i, j; cin >> i >> j; i--; j--; ll nz = zr[i] + zc[j] - za[i][j]; if(nz == zero) cout << s / r[i] / c[j] * a[i][j] << '\n'; else cout << 0 << '\n'; } }