import sys class FenwickTree: def __init__(self, size): self.n = size self.tree = [0] * (self.n + 1) # 1-based indexing def update(self, idx, delta): # Update the idx-th element by delta (1-based index) while idx <= self.n: self.tree[idx] += delta idx += idx & -idx def query(self, idx): # Query the prefix sum from 1 to idx (1-based index) res = 0 while idx > 0: res += self.tree[idx] idx -= idx & -idx return res def main(): input = sys.stdin.read().split() n = int(input[0]) p = list(map(int, input[1:n+1])) if n == 0: print(1) return # Precompute factorial array using optimized method max_fact = 1 for i in range(1, n): max_fact *= i fact = [0] * n if n >= 1: fact[n-1] = max_fact # (n-1)! for the largest term for i in range(n-2, -1, -1): if i+1 == 0: fact[i] = 0 else: fact[i] = fact[i+1] // (i+1) # Initialize Fenwick Tree with all elements present ft = FenwickTree(n) for i in range(1, n+1): ft.update(i, 1) total = 0 for i in range(n): num = p[i] # Count numbers less than 'num' that are still present cnt = ft.query(num - 1) k = n - 1 - i total += cnt * fact[k] # Remove 'num' from available numbers ft.update(num, -1) print(total + 1) if __name__ == "__main__": main()