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 from collections import defaultdict flag = False zr = set() zc = set() for i in range(h): for j in range(w): if A[i][j] == 0: flag = True zr.add(i) zc.add(j) if not flag: 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(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) else: if len(zr) >= 2 and len(zc) >= 2: q = int(input()) for i in range(q): r, c = map(int, input().split()) r, c = r-1, c-1 print(0) elif len(zr) == 1: total = 1 d = defaultdict(lambda: 1) for i in range(h): for j in range(w): if i in zr: continue d[j] *= A[i][j] d[j] %= mod total *= A[i][j] total %= mod q = int(input()) for i in range(q): r, c = map(int, input().split()) r, c = r-1, c-1 if r in zr: ans = total*pow(d[c], mod-2, mod) print(ans%mod) else: print(0) elif len(zc) == 1: total = 1 d = defaultdict(lambda: 1) for i in range(h): for j in range(w): if i in zc: continue d[i] *= A[i][j] d[i] %= mod total *= A[i][j] total %= mod q = int(input()) for i in range(q): r, c = map(int, input().split()) r, c = r-1, c-1 if c in zc: ans = total*pow(d[r], mod-2, mod) print(ans%mod) else: print(0)