h, w = map(int, input().split()) a = [list(map(int, input().split())) for _ in range(h)] b = [1] * h v = [1] * w mod = 10 ** 9 + 7 s = 1 for i in range(h): for j in range(w): s *= a[i][j] s %= mod b[i] *= a[i][j] b[i] %= mod v[j] *= a[i][j] v[j] %= mod for _ in range(int(input())): r, c = map(lambda x: int(x)-1, input().split()) ans = s * pow(b[r], mod-2, mod) * pow(v[c], mod-2, mod) * a[r][c] % mod print(ans)