#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define llint long long #define inf 1e18 #define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++) #define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++) #define chmin(x, y) (x) = min((x), (y)) #define chmax(x, y) (x) = max((x), (y)) #define mod 1000000007 using namespace std; typedef pair P; llint Q; llint h, w; llint a[100005]; llint r[100005], c[100005]; llint modpow(llint a, llint n) { if(n == 0) return 1; if(n % 2){ return ((a%mod) * (modpow(a, n-1)%mod)) % mod; } else{ return modpow((a*a)%mod, n/2) % mod; } } int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> h >> w; llint mul = 1; for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ cin >> a[i*w+j]; mul *= a[i*w+j], mul %= mod; } } for(int i = 0; i < h; i++){ r[i] = 1; for(int j = 0; j < w; j++) r[i] *= a[i*w+j], r[i] %= mod; } for(int i = 0; i < w; i++){ c[i] = 1; for(int j = 0; j < h; j++) c[i] *= a[j*w+i], c[i] %= mod; } cin >> Q; llint x, y; for(int i = 0; i < Q; i++){ cin >> y >> x; x--, y--; llint ans = mul; ans *= modpow(r[y], mod-2), ans %= mod; ans *= modpow(c[x], mod-2), ans %= mod; ans *= a[y*w+x], ans %= mod; cout << ans << endl; } return 0; }