import sys def main(): n = int(sys.stdin.readline()) p = list(map(int, sys.stdin.readline().split())) # Precompute factorial up to (n-1)! fact = [1] * (n) for i in range(1, n): fact[i] = fact[i-1] * i # Fenwick Tree implementation class FenwickTree: def __init__(self, size): self.n = size self.tree = [0] * (self.n + 2) # 1-based indexing 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 ft = FenwickTree(n) # Initialize all elements to 1 (available) for i in range(1, n+1): ft.update(i, 1) ans = 0 for i in range(n): current = p[i] m = n - i - 1 # Number of elements less than current that are available cnt = ft.query(current - 1) ans += cnt * fact[m] # Mark current as used ft.update(current, -1) print(ans + 1) if __name__ == "__main__": main()