def main(): import sys input = sys.stdin.read data = input().split() n = int(data[0]) p = list(map(int, data[1:n+1])) class BIT: def __init__(self, size): self.size = size self.tree = [0] * (self.size + 1) def update(self, idx, delta): while idx <= self.size: 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 # Precompute (n-1)! by multiplying from 1 to n-1 current_fact = 1 for i in range(1, n): current_fact *= i bit = BIT(n) for i in range(1, n+1): bit.update(i, 1) ans = 0 for i in range(n): x = p[i] sum_k = bit.query(x - 1) ans += sum_k * current_fact bit.update(x, -1) remaining = n - i - 1 if remaining > 0: current_fact = current_fact // remaining else: current_fact = 0 print(ans + 1) if __name__ == "__main__": main()