#include using namespace std; #define REP(i,a,n) for(int i=(a); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it) #define ALLOF(c) (c).begin(), (c).end() typedef long long ll; typedef unsigned long long ull; static const ll MOD = 1000000007; int main(){ int H, W; cin >> H >> W; vector> v(H, vector(W)); rep(i,H){ rep(j,W){ cin >> v[i][j]; } } vector> dp1(H, vector(W)); vector> dp2(H, vector(W)); vector> dp3(H, vector(W)); vector> dp4(H, vector(W)); { ll p = 1; for(int y=0; y=0; x--){ p = 1; for(int y=0; y=0; y--){ p *= v[y][0]; p %= MOD; dp3[y][0] = p; } for(int x=1; x=0; y--){ p *= v[y][x]; p %= MOD; dp3[y][x] = (dp3[y][x-1] * p)%MOD; } } } { ll p = 1; for(int y=H-1; y>=0; y--){ p *= v[y][W-1]; p %= MOD; dp4[y][W-1] = p; } for(int x=W-2; x>=0; x--){ p = 1; for(int y=H-1; y>=0; y--){ p *= v[y][x]; p %= MOD; dp4[y][x] = (dp4[y][x+1] * p)%MOD; } } } int Q; cin >> Q; rep(i,Q){ int r, c; cin >> r >> c; r--; c--; ll ret = 1; if(r != 0 && c != 0){ ret *= dp1[r-1][c-1]; ret %= MOD; } if(r != 0 && c != W-1){ ret *= dp2[r-1][c+1]; ret %= MOD; } if(r != H-1 && c != 0){ ret *= dp3[r+1][c-1]; ret %= MOD; } if(r != H-1 && c != W-1){ ret *= dp4[r+1][c+1]; ret %= MOD; } cout << ret << endl; } return 0; }