#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(i, n) repr(i, 0, n) #define INF 2e9 #define MOD 1000000007 //#define MOD 998244353 #define LINF (long long)4e18 #define jck 3.141592 #define PI acos(-1) const double EPS = 1e-10; using ll = long long; using Pi = pair; using Pl = pair; ll modinv(ll a, ll m) { ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } int main(){ int h,w; cin >> h >> w; vector> a(h,vector(w)); rep(i,h)rep(j,w) cin >> a[i][j]; int q; cin >> q; vector> seki(h+1,vector(w+1)); rep(i,h+1) seki[i][0] = 1; rep(i,w+1) seki[0][i] = 1; rep(i,h)rep(j,w){ seki[i+1][j+1] = (a[i][j] == 0 ? 1:a[i][j])*seki[i+1][j]%MOD*seki[i][j+1]%MOD*modinv(seki[i][j],MOD)%MOD; } vector h0(h),w0(w); int cnt = 0; rep(i,h){ rep(j,w){ if(a[i][j] == 0){ h0[i]++; cnt++; } } } rep(i,w){ rep(j,h){ if(a[j][i] == 0){ w0[i]++; cnt++; } } } while(q--){ int r,c; cin >> r >> c; r--; c--; ll ans = seki[h][w]*modinv(seki[r+1][w]*modinv(seki[r][w],MOD)%MOD,MOD)%MOD*modinv(seki[h][c+1]*modinv(seki[h][c]%MOD,MOD),MOD)%MOD*a[r][c]%MOD; if(cnt-h0[r]-w0[c] > 0) cout << 0 << endl; else cout << ans << endl; } }