import sys import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline h, w = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(h)] mod = 1000000007 X = [[1]*(w+1) for i in range(h+1)] for i in range(h): for j in range(w): X[i+1][j+1] = X[i+1][j]*X[i][j+1]*pow(X[i][j],mod-2,mod)*A[i][j] X[i+1][j+1] %= mod def rect(r1, c1, r2, c2): res = X[r2][c2]*pow(X[r2][c1],mod-2, mod)*pow(X[r1][c2],mod-2, mod)*X[r1][c1] return res%mod #print(X) #print(X[h][w]%mod) q = int(input()) for i in range(q): r, c = map(int, input().split()) r, c = r-1, c-1 ans = rect(r+1, c+1, h, w)*rect(0, c+1, r, w)*rect(r+1, 0, h, c)*rect(0, 0, r, c) print(ans%mod)