class FenwickTree: def __init__(self, size): self.size = size self.tree = [0] * (size + 2) # 1-based indexing def update_point(self, index, delta): while index <= self.size: self.tree[index] += delta index += index & -index def query_prefix(self, index): res = 0 while index > 0: res += self.tree[index] index -= index & -index return res n = int(input()) p = list(map(int, input().split())) # Precompute factorials up to (n-1)! fact = [1] * n for i in range(1, n): fact[i] = fact[i-1] * i fenwick = FenwickTree(n) for i in range(1, n+1): fenwick.update_point(i, 1) total = 0 for i in range(n): current_p = p[i] x = fenwick.query_prefix(current_p - 1) exponent = (n-1) - i if exponent < 0: exponent = 0 total += x * fact[exponent] fenwick.update_point(current_p, -1) print(total + 1)