import sys
inpt = sys.stdin.readline
from itertools import accumulate
mod = 10**9+7

h, w = map(int, input().split())
A = tuple(tuple(map(int, input().split())) for _ in range(h))
row_cumsum = [1]*h
col_cumsum = [1]*w
row_zero = [0]*h
col_zero = [0]*w
zero_cnt = 0
temp = 1
for i, L in enumerate(A):
    for j, a in enumerate(L):
        if a == 0:
            zero_cnt += 1
            row_zero[i] += 1
            col_zero[j] += 1
        row_cumsum[i] *= a
        row_cumsum[i] %= mod
        col_cumsum[j] *= a
        col_cumsum[j] %= mod
        temp *= a
        temp %=mod
        if i == h-1:
            col_cumsum[j] = pow(col_cumsum[j], mod-2, mod)
    row_cumsum[i] = pow(row_cumsum[i], mod-2, mod)
q = int(input())
if temp:
    for _ in range(q):
        r, c = map(int, input().split())
        r -= 1
        c -= 1
        ans = temp * row_cumsum[r] * col_cumsum[c] * A[r][c]
        ans %= mod
        print(ans)
else:
    for i in range(h):
        for j in range(w):
            if row_zero[i]+col_zero[j]-(A[i][j]==0) == zero_cnt:
                other = 1
                for s in range(h):
                    for t in range(w):
                        if s == i or t == j:
                            continue
                        other *= A[s][t]
                        other %= mod
                for _ in range(q):
                    r, c = map(int, input().split())
                    r -= 1
                    c -= 1
                    if r == i and c == j:
                        print(other)
                    else:
                        print(0)
                exit()
    for _ in range(q):
        print(0)