def Binit(B,siz): while len(B)siz+1: del B[-1] for i in range(siz+1): B[i]=0 B.append(siz) def Badd(B,a,x): z=a while z<=B[-1]: B[z]+=x z+=(z&(-z)) def Bsum(B,a): r=0 z=a while z>0: r+=B[z] z-=(z&(-z)) return r def Bssum(B,a,b): return Bsum(B,max(a,b))-Bsum(B,min(a,b)-1) BIT=[] BC=[] N=int(input()) A=list(map(int,input().split())) Binit(BIT,N+1) Binit(BC,N+1) D=dict() B=sorted(set(A)) for i in range(len(B)): D[B[i]]=i+1 C=[0]*N Z=[0]*N X=0 for i in range(N): C[i]=X-Bsum(BIT,D[A[i]])+(i-Bsum(BC,D[A[i]]))*A[i] X+=A[i] Z[i]=i-Bsum(BC,D[A[i]]) Badd(BC,D[A[i]],1) Badd(BIT,D[A[i]],A[i]) X=0 P=0 Binit(BIT,N+1) Binit(BC,N+1) Y=0 for i in range(N): P+=X-Bsum(BIT,D[A[i]])+(Y-Bsum(BC,D[A[i]]))*A[i] X+=C[i] Y+=Z[i] Badd(BC,D[A[i]],Z[i]) Badd(BIT,D[A[i]],C[i]) print(P%998244353)