#include #include using namespace std; const long mod=1e9+7; int H,W; main() { cin>>H>>W; vector >A(H,vector(W)); for(int i=0;i>A[i][j]; vector >UL(H,vector(W)); vector >UR(H,vector(W)); vector >DL(H,vector(W)); vector >DR(H,vector(W)); for(int i=0;i>Q; for(;Q--;) { int x,y;cin>>x>>y;x--,y--; long ans=1; if(x-1>=0&&y-1>=0)ans=ans*UL[x-1][y-1]%mod; if(x-1>=0&&y+1=0)ans=ans*DL[x+1][y-1]%mod; if(x+1