#include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; #define int long long #define double long double typedef vector VI; typedef pair pii; typedef vector VP; typedef vector VS; typedef priority_queue PQ; templatebool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } templatebool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #define fore(i,a) for(auto &i:a) #define REP(i,n) for(int i=0;i, greater > q2; ll modpow(ll a, ll n = mod - 2) { ll r = 1; while (n) r = r * ((n % 2) ? a : 1) % mod, a = a * a%mod, n >>= 1; return r; } long long modinv(long long a) { return modpow(a, mod - 2); } signed main() { cin.tie(0); ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector A(H, VI(W)); REP(i, H)REP(j, W)cin >> A[i][j]; int sum = 1; VI a(H, 1), b(W, 1); VI zeroh(H, 0), zerow(W, 0); int zero = 0; REP(i, H) { REP(j, W) { if (A[i][j] == 0) { zero++; zeroh[i]++; zerow[j]++; } else { a[i] *= A[i][j]; b[j] *= A[i][j]; sum *= A[i][j]; a[i] %= mod; b[j] %= mod; sum %= mod; } } } int ans; int Q; cin >> Q; while (Q--) { int R, C; cin >> R >> C; R--; C--; ans = sum; int tmp = zero - zeroh[R] - zerow[C]; if (A[R][C] == 0)tmp++; if (tmp > 0) { cout << 0 << endl; continue; } ans *= modinv(a[R]); ans %= mod; ans *= modinv(b[C]); ans %= mod; if (A[R][C] != 0) { ans *= A[R][C]; ans %= mod; } cout << ans << '\n'; } return 0; }