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 B=int(len(z)**0.5)+1 st1=[0]*B*B st2=[0]*B lc=[0]*n for i in range(B*B): st1[i]=0 st2[i//B]=0 for i in range(n): v=a[i] g=0 l=d[v]+1 r=n+2 yl=l//B yr=r//B if yl==yr: g+=sum(st1[l:r+1]) else: g+=sum(st1[l:yl*B+B]) g+=sum(st2[yl+1:yr]) g+=sum(st1[yr*B:r+1]) lc[i]=g st1[d[v]]+=1 st2[d[v]//B]+=1 rc=[0]*n for i in range(B*B): st1[i]=0 st2[i//B]=0 for i in reversed(range(n)): v=a[i] g=0 l=0 r=d[v]-1 yl=l//B yr=r//B if yl==yr: g+=sum(st1[l:r+1]) else: g+=sum(st1[l:yl*B+B]) g+=sum(st2[yl+1:yr]) g+=sum(st1[yr*B:r+1]) rc[i]=g st1[d[v]]+=1 st2[d[v]//B]+=1 lc2=[0]*n for i in range(B*B): st1[i]=0 st2[i//B]=0 for i in range(n): v=a[i] g=0 l=d[v]+1 r=n+2 yl=l//B yr=r//B if yl==yr: g+=sum(st1[l:r+1]) else: g+=sum(st1[l:yl*B+B]) g+=sum(st2[yl+1:yr]) g+=sum(st1[yr*B:r+1]) lc2[i]=g st1[d[v]]+=lc[i] st2[d[v]//B]+=lc[i] rc2=[0]*n for i in range(B*B): st1[i]=0 st2[i//B]=0 for i in reversed(range(n)): v=a[i] g=0 l=0 r=d[v]-1 yl=l//B yr=r//B if yl==yr: g+=sum(st1[l:r+1]) else: g+=sum(st1[l:yl*B+B]) g+=sum(st2[yl+1:yr]) g+=sum(st1[yr*B:r+1]) rc2[i]=g st1[d[v]]+=rc[i] st2[d[v]//B]+=rc[i] ans=0 ans+=sum(a[i]*rc2[i] for i in range(n))%M ans+=sum(a[i]*lc[i]*rc[i] for i in range(n))%M ans+=sum(a[i]*lc2[i] for i in range(n))%M print(ans%M)