import sys import pypyjit import itertools import heapq import math from collections import deque, defaultdict import bisect input = sys.stdin.readline sys.setrecursionlimit(10 ** 6) pypyjit.set_param('max_unroll_recursion=-1') def index_lt(a, x): 'return largest index s.t. A[i] < x or -1 if it does not exist' return bisect.bisect_left(a, x) - 1 def index_le(a, x): 'return largest index s.t. A[i] <= x or -1 if it does not exist' return bisect.bisect_right(a, x) - 1 def index_gt(a, x): 'return smallest index s.t. A[i] > x or len(a) if it does not exist' return bisect.bisect_right(a, x) def index_ge(a, x): 'return smallest index s.t. A[i] >= x or len(a) if it does not exist' return bisect.bisect_left(a, x) class CumulativeSum2D: def __init__(self, A): self.A = A self.H = len(A) self.W = len(A[0]) self.S = [[0 for _ in range(self.W + 1)] for _ in range(self.H + 1)] for i in range(self.H): for j in range(self.W): self.S[i + 1][j + 1] = self.S[i + 1][j] + self.S[i][j + 1] - self.S[i][j] + self.A[i][j] def sum(self, lx, ly, rx, ry): if lx > rx or ly > ry: return 0 return self.S[rx + 1][ry + 1] - self.S[lx][ry + 1] - self.S[rx + 1][ly] + self.S[lx][ly] H, W = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(H)] mod = 10 ** 9 + 7 R = [] for i in range(H): acc = 1 for j in range(W): acc *= A[i][j] acc %= mod R.append(acc) C = [] for j in range(W): acc = 1 for i in range(H): acc *= A[i][j] acc %= mod C.append(acc) S = 1 for i in range(H): S *= R[i] S %= mod is_zero = [[1 if A[i][j] == 0 else 0 for j in range(W)] for _ in range(H)] CZ = CumulativeSum2D(is_zero) Q = int(input()) for _ in range(Q): r, c = map(lambda x: int(x) - 1, input().split()) zero = 0 zero += CZ.sum(0, 0, r - 1, c - 1) zero += CZ.sum(0, c + 1, r - 1, W - 1) zero += CZ.sum(r + 1, 0, H - 1, c - 1) zero += CZ.sum(r + 1, c + 1, H - 1, W - 1) if zero > 0: print(0) else: print(S * pow(R[r], mod - 2, mod) * pow(C[c], mod - 2, mod) * A[r][c] % mod)