#include "bits/stdc++.h" using namespace std; #define ll long long #define rep(i,n) for(ll i = 0; i < (n); i++) const int inf = 1e9 + 11; int MOD = 1e9 + 7; const ll INF = 1LL << 60; 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 { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= 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 { mint res(*this); return res /= a; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int h, w; cin >> h >> w; mint ans = 1; vector>a(h, vector(w)); bool ok = false; int cnt = 0; rep(i, h)rep(j, w)cin >> a[i][j]; rep(i, h)rep(j, w) { if (a[i][j] == 0) { ok = true; cnt++; continue; } ans *= a[i][j]; } vectorr(h, 1), c(w, 1); vectorisr(h), isc(h); rep(i, h) { rep(j, w) { if (a[i][j] == 0) { isr[i]++; continue; } r[i] *= a[i][j]; } } rep(j, w) { rep(i, h) { if (a[i][j] == 0) { isc[j]++; continue; } c[j] *= a[i][j]; } } int q; cin >> q; rep(_, q) { ll x, y; cin >> x >> y; x--; y--; if (!ok) { mint now = ans; mint tmp = r[x]; tmp *= c[y]; tmp /= a[x][y]; now /= tmp; cout << now.x << endl; } else { if (isr[x] || isc[y]) { if (isr[x] + isc[y] - (int)(a[x][y] == 0)== cnt) { mint now = ans; mint tmp = r[x]; tmp *= c[y]; tmp /= a[x][y]; now /= tmp; cout << now.x << endl; } else cout << 0 << endl; } else { cout << 0 << endl; } } } return 0; }