import sys input = sys.stdin.readline MOD = 998244353 class BIT: def __init__(self, n, array): self.size = n self.bit = [0] * (n + 1) def add(self, k, p): t = k + 1 while(t <= self.size): self.bit[t] += p self.bit[t] %= MOD t += t & -t def sum(self, r): res, t = 0, r while(t > 0): res += self.bit[t] res %= MOD t -= t & -t return res """ Main Code """ n = int(input()) a = list(map(int,input().split())) s = sorted(set(a)) d = {} for i in range(len(s)): d[s[i]] = i for i in range(n): a[i] = d[a[i]] b = [len(s) - 1 - i for i in a] lbt = BIT(n,a); lcnt = [0] * n rbt = BIT(n,b); rcnt = [0] * n for i in range(n): lbt.add(a[i],1) rbt.add(b[n - 1 - i],1) lcnt[i] = lbt.sum(a[i]) rcnt[n - 1 - i] = rbt.sum(b[n - 1 - i]) bt = BIT(n,b) ans = 0 for i in range(n): bt.add(b[i],lcnt[i]) ans += bt.sum(b[i]) * rcnt[i] ans %= MOD print(ans)