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)] total0 = 0 row0 = [0]*h col0 = [0]*w for i in range(h): for j in range(w): if A[i][j] != 0: X[i+1][j+1] = X[i+1][j]*X[i][j+1]*pow(X[i][j],mod-2,mod)*A[i][j] else: X[i+1][j+1] = X[i+1][j]*X[i][j+1]*pow(X[i][j],mod-2,mod)*1 total0 += 1 row0[i] += 1 col0[j] += 1 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 z = total0-row0[r]-col0[c] if A[r][c] == 0: z += 1 if z > 0: print(0) else: 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)