class FenwickTree: def __init__(self, size): self.n = size self.tree = [0] * (self.n + 1) # 初始化每个位置为1,表示可用 for i in range(1, self.n + 1): self.update(i, 1) def update(self, idx, delta): while idx <= self.n: self.tree[idx] += delta idx += idx & -idx def query(self, idx): res = 0 while idx > 0: res += self.tree[idx] idx -= idx & -idx return res def main(): import sys input = sys.stdin.read().split() n = int(input[0]) p = list(map(int, input[1:n+1])) # 预先计算阶乘数组 max_fact = n - 1 fact = [1] * (max_fact + 1) for i in range(1, max_fact + 1): fact[i] = fact[i-1] * i fenwick = FenwickTree(n) ans = 0 for i in range(n): current = p[i] c = fenwick.query(current - 1) m = n - i - 1 ans += c * fact[m] fenwick.update(current, -1) print(ans + 1) if __name__ == "__main__": main()