import sys from collections import deque, Counter input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) inf = 2 ** 63 - 1 mod = 998244353 class fenwick_tree(): n=1 data=[0 for i in range(n)] def __init__(self,N): self.n=N self.data=[0 for i in range(N)] def add(self,p,x): assert 0<=p0): s+=self.data[r-1] r-=r&-r return s def __getitem__(self, p): if isinstance(p, int): return self.sum(p, p + 1) else: return self.sum(p.start, p.stop) def __setitem__(self, p, x): return self.add(p, x - self[p]) n = ii() a = li() sa = list(set(a)) A = [(sa[i], i) for i in range(len(sa))] A.sort() d = {} sn = len(A) for i in range(sn): d[A[i][0]] = i f1 = fenwick_tree(sn) c1 = fenwick_tree(sn) f2 = fenwick_tree(sn) c2 = fenwick_tree(sn) ans = 0 for i in range(n): f2[d[a[i]]] += a[i] c2[d[a[i]]] += 1 for i in range(n): f2[d[a[i]]] -= a[i] c2[d[a[i]]] -= 1 ans += f1[d[a[i]] + 1: sn] * c2[0:d[a[i]]] + f2[0:d[a[i]]] * c1[d[a[i]] + 1: sn] + c1[d[a[i]] + 1: sn] * c2[0:d[a[i]]] % mod * a[i] ans %= mod f1[d[a[i]]] += a[i] c1[d[a[i]]] += 1 print(ans)