class FenwickTree: def __init__(self,n): self.n=n self.st=[0]*n def add(self,p,x): p+=1 while p<=self.n: self.st[p-1]+=x p+=p&(-p) def sum(self,l,r): return self._sum(r)-self._sum(l) def _sum(self,p): a=0 while p>0: a+=self.st[p-1] p-=p&(-p) return a n=int(input()) a=list(map(int,input().split())) z=sorted(set(a+[-1,10**10])) d={v:i for i,v in enumerate(z)} M=998244353 lc=[0]*n ls=[0]*n stc=FenwickTree(len(z)) sts=FenwickTree(len(z)) for i in range(n): v=a[i] l=d[v]+1 r=len(z)-1 lc[i]=stc.sum(l,r+1) ls[i]=sts.sum(l,r+1) stc.add(d[v],1) sts.add(d[v],v) rc=[0]*n rs=[0]*n stc=FenwickTree(len(z)) sts=FenwickTree(len(z)) for i in reversed(range(n)): v=a[i] l=0 r=d[v]-1 rc[i]=stc.sum(l,r+1) rs[i]=sts.sum(l,r+1) stc.add(d[v],1) sts.add(d[v],v) print(sum(ls[i]*rc[i]+lc[i]*a[i]*rc[i]+lc[i]*rs[i] for i in range(n))%M)