import sys input = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 7) mod = 10 ** 9 + 7 H, W = map(int, input().split()) A = tuple(tuple(map(int, input().split())) for _ in range(H)) ALL = 1 row = [1] * H col = [1] * W for i, ai in enumerate(A): for j, a in enumerate(ai): row[i] *= a col[j] *= a ALL *= a row[i] %= mod col[j] %= mod ALL %= 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) Q = int(input()) for _ in range(Q): r, c = map(int, input().split()) r -= 1 c -= 1 res = ALL * row[r] * col[c] * A[r][c] % mod print(res)