#include #define rep(i, n) for(int i=0; i<(int)(n); i++) using namespace std; using ll = long long; using P = pair; const int d = 1000000007; int main(){ int h, w; cin >> h >> w; int a[h][w]; // ll ans = 1; rep(i,h)rep(j,w){ cin >> a[i][j]; // ans *= a[i][j]; } int q; cin >> q; rep(i,q){ int r, c; ll ans = 1; int cnt = 0; cin >> r >> c; --r; --c; rep(j,h)rep(k,w){ if(j == r || k == c) continue; ans *= a[j][k]; cnt++; if(cnt == 1) continue; ans %= d; // if(j == r) ans /= a[r][k]; // if(k == c) ans /= a[j][c]; } cout << ans << endl; } return 0; }