class BIT: def __init__(self, n, element): self.num = n self.ele = element self.data = [self.ele] * (self.num + 1) def calc(self, x, y): return x * y % (10 ** 9 + 7) def update(self, idx, x): while idx <= self.num: self.data[idx] = self.calc(self.data[idx], x) idx += idx & -idx def sum(self, idx): res = self.ele while idx > 0: res = self.calc(res, self.data[idx]) idx -= idx & -idx return res def prod(self, l, r): mod = 10 ** 9 + 7 return self.sum(r) * pow(self.sum(l), mod-2, mod) % mod h, w = map(int, input().split()) a = [list(map(int, input().split())) for _ in range(h)] mod = 10 ** 9 + 7 if h < w: st = [BIT(w, 1) for _ in range(h)] for i in range(h): for j in range(w): st[i].update(j+1, a[i][j]) for _ in range(int(input())): r, c = map(lambda x: int(x)-1, input().split()) ans = 1 for i in range(h): if i == r: continue ans *= st[i].prod(0, c) * st[i].prod(c+1, w) % mod ans %= mod print(ans) else: st = [BIT(h, 1) for _ in range(w)] for i in range(h): for j in range(w): st[j].update(i+1, a[i][j]) for _ in range(int(input())): r, c = map(lambda x: int(x)-1, input().split()) ans = 1 for j in range(w): if j == c: continue ans *= st[j].prod(0, r) * st[j].prod(r+1, h) % mod ans %= mod print(ans)