class FenwickTree:
    def __init__(self, size):
        self.n = size
        self.tree = [0] * (self.n + 2)  # Use 1-based indexing

    def update(self, idx, delta=1):
        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
    data = input().split()
    
    n = int(data[0])
    p = list(map(int, data[1:n+1]))
    
    if n == 0:
        print(1)
        return
    
    # Precompute factorials up to (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
    
    ft = FenwickTree(n)
    for i in range(1, n + 1):
        ft.update(i)
    
    result = 0
    for i in range(n):
        current = p[i]
        # Number of available elements less than 'current'
        count = ft.query(current - 1)
        # Factorial term (n - i - 1)!
        if (n - i - 1) >= 0:
            factorial = fact[n - i - 1]
        else:
            factorial = 1
        result += count * factorial
        # Mark 'current' as used
        ft.update(current, -1)
    
    print(result + 1)

if __name__ == "__main__":
    main()