#include using namespace std; using ll = long long; const ll mod = 1000000007; long long modinv(long long a, long long m) { long long b = m, u = 1, v = 0; while (b) { long long 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> mp(h, vector(w)); vector preh(h); vector prew(w); ll ans = 1; for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ cin >> mp[i][j]; ans = ans * mp[i][j] % mod; } } for(int i = 0; i < h; i++){ preh[i] = 1; for(int j = 0; j < w; j++){ preh[i] *= mp[i][j]; preh[i] %= mod; } } for(int i = 0; i < w; i++){ prew[i] = 1; for(int j = 0; j < h; j++){ prew[i] *= mp[j][i]; prew[i] %= mod; } } int q; cin >> q; while(q--){ ll r, c; cin >> r >> c; r--; c--; ll a = modinv(preh[r], mod); ll b = modinv(prew[c], mod); ll ans1 = ans; ans1 = ((ans1 * a % mod) * b % mod) * mp[r][c] % mod; cout << (mod + ans1 % mod) % mod << endl; } }