h,w = map(int,input().split()) A = [list(map(int,input().split())) for i in range(h)] mod = 10**9+7 q = int(input()) Q = [list(map(int,input().split())) for i in range(q)] ans = [1]*q accum = [[1]*(w+2) for i in range(h+2)] for i in range(h): for j in range(w): accum[i+1][j+1] = accum[i+1][j]*A[i][j] accum[i+1][j+1] %= mod for i in range(h): for j in range(w): accum[i+1][j+1] *= accum[i][j+1] accum[i+1][j+1] %= mod for i,(r,c) in enumerate(Q): ans[i] *= accum[r-1][c-1] accum = [[1]*(w+2) for i in range(h+2)] for i in range(h)[::-1]: for j in range(w)[::-1]: accum[i+1][j+1] = accum[i+1][j+2]*A[i][j] accum[i+1][j+1] %= mod for i in range(h)[::-1]: for j in range(w)[::-1]: accum[i+1][j+1] *= accum[i+2][j+1] accum[i+1][j+1] %= mod for i,(r,c) in enumerate(Q): ans[i] *= accum[r+1][c+1] accum = [[1]*(w+2) for i in range(h+2)] for i in range(h): for j in range(w)[::-1]: accum[i+1][j+1] = accum[i+1][j+2]*A[i][j] accum[i+1][j+1] %= mod for i in range(h): for j in range(w)[::-1]: accum[i+1][j+1] *= accum[i][j+1] accum[i+1][j+1] %= mod for i,(r,c) in enumerate(Q): ans[i] *= accum[r-1][c+1] accum = [[1]*(w+2) for i in range(h+2)] for i in range(h)[::-1]: for j in range(w): accum[i+1][j+1] = accum[i+1][j]*A[i][j] accum[i+1][j+1] %= mod for i in range(h)[::-1]: for j in range(w): accum[i+1][j+1] *= accum[i+2][j+1] accum[i+1][j+1] %= mod for i,(r,c) in enumerate(Q): ans[i] *= accum[r+1][c-1] for i in ans: print(i%mod)