#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef DEBUG #include "library/Utility/debug.cpp" #else #define debug(...) #endif #define rep(i,n) for(int i=0;i<(n);++i) #define EL '\n' #define print(i) std::cout << (i) << '\n' #define all(v) (v).begin(), (v).end() using lnt = long long; struct FIO{FIO(){std::cin.tie(0);std::cout.tie(0);std::ios_base::sync_with_stdio(0);std::cout<=MOD) v-=MOD; return *this; } mint& operator-=(const mint& a){ if((v+=MOD-a.v)>=MOD) v-=MOD; return *this; } mint& operator*=(const mint& a){ (v*=a.v)%=MOD; return *this; } mint& operator/=(const mint& a){ (*this) *= a.inv(); return *this; } mint operator+(const mint& a)const{ return mint(*this) += a; } mint operator-(const mint& a)const{ return mint(*this) -= a; } mint operator*(const mint& a)const{ return mint(*this) *= a; } mint operator/(const mint& a)const{ return mint(*this) /= a; } bool operator<(const mint& a)const{ return v < a.v; } bool operator==(const mint& a)const{ return v == a.v; } mint pow(lnt k)const{ mint r(1),t(v); while(k){ if(k&1) r*=t; t*=t; k>>=1; } return r; } mint inv()const{ return pow(MOD-2); } static mint comb(lnt n, lnt k) { if(n-k construct_comb(int n) { std::vector c(n+1); mint a = 1; c[0] = a; for(int i=1;i<=n;i++) { a = a*mint(n+1-i)/i; c[i] = a; } return c; } static std::vector construct_fact(int n) { std::vector f(n+1,1); for(int i=2;i<=n;i++) f[i]=f[i-1]*i; return f; } }; std::istream& operator>>(std::istream&i,mint&a){ lnt t; i>>t; a=mint(t); return i; } std::ostream& operator<<(std::ostream&o,const mint&a){ o<> h >> w; std::vector > g(h,std::vector(w)); rep(i,h) rep(j,w) std::cin >> g[i][j]; std::vector > > A(4,std::vector >(h,std::vector(w))); rep(k,4) rep(i,h) rep(j,w) A[k][i][j]=mint(g[i][j]); rep(k,4) { rep(x,h) rep(y,w) { int i=(k&1)?x:h-x-1; int j=(k>=2)?y:w-y-1; if(x==0&&y==0) A[k][i][j]=mint(g[i][j]); else if(x==0) A[k][i][j]=mint(g[i][j])*A[k][i][(k>1)?j-1:j+1]; else if(y==0) A[k][i][j]=mint(g[i][j])*A[k][(k&1)?i-1:i+1][j]; else { A[k][i][j]=A[k][(k&1)?i-1:i+1][j]*mint(g[i][j])*A[k][i][(k>=2)?j-1:j+1]/A[k][(k&1)?i-1:i+1][(k>=2)?j-1:j+1]; } } } debug(A[0]); int q; std::cin >> q; rep(qq,q) { int r,c; std::cin >> r >> c; r--;c--; mint ans=1; debug(r,c); if(r>0&&c>0) ans*=A[3][r-1][c-1]; if(r>0&&c0) ans*=A[2][r+1][c-1]; if(r