#include #define rep(i,n) for(int i=0;i<(int)(n);i++) using namespace std; using ll = long long ; using P = pair ; using pll = pair; constexpr int INF = 1e9; constexpr long long LINF = 1e17; constexpr int MOD = 1000000007; constexpr double PI = 3.14159265358979323846; template struct ModInt{ long long x=0; constexpr ModInt(long long x=0):x((x%mod+mod)%mod){} constexpr ModInt operator+(const ModInt& r)const{return ModInt(*this)+=r;} constexpr ModInt operator-(const ModInt& r)const{return ModInt(*this)-=r;} constexpr ModInt operator*(const ModInt& r)const{return ModInt(*this)*=r;} constexpr ModInt operator/(const ModInt& r)const{return ModInt(*this)/=r;} constexpr ModInt& operator+=(const ModInt& r){ if((x+=r.x)>=mod) x-=mod; return *this;} constexpr ModInt& operator-=(const ModInt& r){ if((x-=r.x)<0) x+=mod; return *this;} constexpr ModInt& operator*=(const ModInt& r){ if((x*=r.x)>=mod) x%=mod; return *this;} constexpr ModInt& operator/=(const ModInt& r){ return *this*=r.inv();} ModInt inv() const { long long s=x,sx=1,sy=0,t=mod,tx=0,ty=1; while(s%t!=0){ long long temp=s/t,u=s-t*temp,ux=sx-temp*tx,uy=sy-temp*ty; s=t;sx=tx;sy=ty; t=u;tx=ux;ty=uy; } return ModInt(tx); } ModInt pow(long long n) const { ModInt a=1; while(n>0){ if(n&1) a*=*this; *this*=*this; n>>=1; } return a; } friend constexpr ostream& operator<<(ostream& os,const ModInt& a) {return os << a.x;} friend constexpr istream& operator>>(istream& is,ModInt& a) {return is >> a.x;} }; using mint = ModInt; int main(){ int h,w; cin >> h >> w; vector> a(h,vector(w,0)); rep(i,h)rep(j,w) cin >> a[i][j]; vector> ul(h,vector(w,0)); vector> ur(h,vector(w,0)); vector> dl(h,vector(w,0)); vector> dr(h,vector(w,0)); rep(i,h)rep(j,w){ ul[i][j]=ur[i][j]=dl[i][j]=dr[i][j]=a[i][j]; } for(int i=0;i=0;j--){ ur[i][j] *= ur[i][j+1]; } } for(int j=0;j=0;i--){ dl[i][j] *= dl[i+1][j]; } } for(int i=0;i=0;j--){ dr[i][j] *= dr[i][j+1]; } } for(int j=0;j=0;i--){ dr[i][j] *= dr[i+1][j]; } } int q; cin >> q; while(q--){ int r,c; cin >> r >> c; --r;--c; mint ans = 1; if(r-1>=0&&c-1>=0) ans *= ul[r-1][c-1]; if(r-1>=0&&c+1=0) ans *= dl[r+1][c-1]; if(r+1