def main(): n = int(input()) p = list(map(int, input().split())) bit = [0] * (n+1) ans = 1 a = 1 for i in range(1, n+1): x = p[n-i] v = 0 while x > 0: v = v + bit[x] x = x - (x&-x) t = a * v ans= t + ans x = p[n-i] while x <= n: bit[x] = bit[x] + 1 x = x + (x&-x) a = a * i print(ans) main()