import sys; input = sys.stdin.buffer.readline sys.setrecursionlimit(10**7) from collections import defaultdict con = 10 ** 9 + 7; INF = float("inf") def getlist(): return list(map(int, input().split())) #処理内容 def main(): H, W = getlist() table = [] for i in range(H): A = getlist() table.append(A) Hsum = [1] * H Wsum = [1] * W Hzero = [0] * H Wzero = [0] * W zerocnt = 0 ALL = 1 for i in range(H): for j in range(W): if table[i][j] == 0: Hzero[i] += 1 Wzero[j] += 1 zerocnt += 1 else: ALL = (ALL * table[i][j]) % con Wsum[j] = (Wsum[j] * table[i][j]) % con Hsum[i] = (Hsum[i] * table[i][j]) % con # print(Hsum) # print(Wsum) # print(ALL) # クエリ Q = int(input()) for i in range(Q): r, c = getlist() r -= 1; c -= 1 z = Hzero[r] + Wzero[c] if table[r][c] == 0: z -= 1 if z == zerocnt: ans = ALL * pow(Hsum[r], con - 2, con) * pow(Wsum[c], con - 2, con) if table[r][c] != 0: ans *= table[r][c] ans %= con print(ans) else: print(0) if __name__ == '__main__': main()