MOD = 1000000007 modinv = lambda a, mod=MOD: pow(a, -1, mod) int1 = lambda x: int(x) - 1 H, W = map(int, input().split()) A = [tuple(map(int, input().split())) for _ in range(H)] R, C = [1] * H, [1] * W P = 1 for i in range(H): for j in range(W): R[i] = R[i] * A[i][j] % MOD C[j] = C[j] * A[i][j] % MOD P = P * A[i][j] % MOD Q = int(input()) for _ in range(Q): r, c = map(int1, input().split()) print(P * modinv(R[r]) * modinv(C[c]) * A[r][c] % MOD)