from collections import defaultdict import sys input = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 7) mod = 10 ** 9 + 7 H, W = map(int, input().split()) A = list(list(map(int, input().split())) for _ in range(H)) zero = 0 cnt_row = [0] * H cnt_col = [0] * W ALL = 1 row = [1] * H col = [1] * W for i, ai in enumerate(A): for j, a in enumerate(ai): if a == 0: zero += 1 cnt_row[i] += 1 cnt_col[j] += 1 else: ALL = ALL * a % mod row[i] = row[i] * a % mod col[j] = col[j] * a % mod for i in range(H): row[i] = pow(row[i], mod-2, mod) for j in range(W): col[j] = pow(col[j], mod-2, mod) safe = set() dr = defaultdict(list) dc = defaultdict(list) for i, c in enumerate(cnt_row): dr[c].append(i) for i, c in enumerate(cnt_col): dc[c].append(i) for k, v in dr.items(): for c in dc[zero - k]: for r in v: if A[r][c] != 0: safe.add((r, c)) for c in dc[zero - k + 1]: for r in v: if A[r][c] == 0: safe.add((r, c)) Q = int(input()) for _ in range(Q): r, c = map(int, input().split()) r -= 1 c -= 1 if zero == 0 or (r, c) in safe: res = ALL * row[r] * col[c] % mod if A[r][c] != 0: res *= A[r][c] print(res % mod) else: print(0)