#include #include #include #include #include #include #include using namespace std; using ll = long long; constexpr int P = 1000000007; ll modinv(int n) { int m = P, y = 0, v = 1; while (1) { int q = m / n; int r = m % n; if (r == 0) return v < 0 ? v + P : v; y -= v * q; swap(y, v); m = n; n = r; } } int main() { ios::sync_with_stdio(false); cin.tie(0); int h, w; cin >> h >> w; vector x(h), y(w, 1), z(h * w); vector x0(h), y0(w, 0); ll s = 1; int s0 = 0; for (int i = 0; i < h; i++) { ll t = 1; int t0 = 0; for (int j = 0; j < w; j++) { int a; cin >> a; z[i * w + j] = a; if (a == 0) { t0++; y0[j]++; } else { (t *= a) %= P; y[j] = y[j] * a % P; } } x[i] = t; x0[i] = t0; (s *= t) %= P; s0 += t0; } int q; cin >> q; while (q--) { int i, j; cin >> i >> j; i--; j--; int a = z[i * w + j]; if (s0 - x0[i] - y0[j] + (a == 0) == 0) { int r = s * modinv((ll)x[i] * y[j] % P) % P; if (a != 0) r = (ll)r * a % P; cout << r << '\n'; } else { cout << 0 << '\n'; } } return 0; }