class segt: def __init__(self, n, ele, calc): self.num = 2 ** (n - 1).bit_length() self.el = ele self.data = [ele] * (2 * self.num) self.calc = calc def update(self, idx, x): idx += self.num - 1 self.data[idx] = x while idx > 0: idx = (idx - 1) // 2 self.data[idx] = self.calc(self.data[2*idx+1], self.data[2*idx+2]) def renew(self, idx, x): self.update(idx, self.calc(self.get(idx), x)) def prod(self, left, right): l = left + self.num r = right + self.num res = self.el while l < r: if l % 2: res = self.calc(res, self.data[l-1]) l += 1 if r % 2: r -= 1 res = self.calc(res, self.data[r-1]) l //= 2 r //= 2 return res def get(self, idx): return self.data[idx+self.num-1] def mul(x, y): return x * y % (10 ** 9 + 7) h, w = map(int, input().split()) a = [list(map(int, input().split())) for _ in range(h)] mod = 10 ** 9 + 7 if h < w: st = [segt(w, 1, mul) for _ in range(h)] for i in range(h): for j in range(w): st[i].update(j, 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 = [segt(h, 1, mul) for _ in range(w)] for i in range(h): for j in range(w): st[j].update(i, 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)