from collections import defaultdict 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 N=int(input()) A=[int(i) for i in input().split()] B=sorted([(A[i],i) for i in range(N)]) I=[B[i][1] for i in range(N)] GL=fenwick_tree(N) GR=fenwick_tree(N) SL=fenwick_tree(N) SR=fenwick_tree(N) GL0=[0 for i in range(N)] GR0=[0 for i in range(N)] SL0=[0 for i in range(N)] SR0=[0 for i in range(N)] for i in range(N-1,-1,-1): GL0[I[i]]=GL.sum(0,I[i]) SL0[I[i]]=SL.sum(0,I[i]) GL.add(I[i],1) SL.add(I[i],A[I[i]]) for i in range(N): GR0[I[i]]=GR.sum(I[i],N) SR0[I[i]]=SR.sum(I[i],N) GR.add(I[i],1) SR.add(I[i],A[I[i]]) L=defaultdict(int) R=defaultdict(int) ''' for i in range(N-1,-1,-1): GL0[i]-=L[A[i]] SL0[i]-=A[i]*L[A[i]] L[A[i]]+=1 for i in range(N): GR0[i]-=R[A[i]] SR0[i]-=A[i]*R[A[i]] R[A[i]]+=1 print(GL0) print(GR0) print(SL0) print(SR0) ''' ans=0 mod=998244353 for i in range(N): ans+=GL0[i]*SR0[i]+SL0[i]*GR0[i]+A[i]*GL0[i]*GR0[i] ans%=mod print(ans)