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(d)**0.5)+1 lc=[0]*n ls=[0]*n stlc1=[0]*B*B stlc2=[0]*B stls1=[0]*B*B stls2=[0]*B rc=[0]*n rs=[0]*n strc1=[0]*B*B strc2=[0]*B strs1=[0]*B*B strs2=[0]*B for p in range(n): i=p v=a[i] g=0 l=d[v]+1 r=n+2 yl=l//B yr=r//B if yl==yr: g+=sum(stlc1[l:r+1]) else: g+=sum(stlc1[l:yl*B+B]) g+=sum(stlc2[yl+1:yr]) g+=sum(stlc1[yr*B:r+1]) lc[i]=g stlc1[d[v]]+=1 stlc2[d[v]//B]+=1 g=0 l=d[v]+1 r=n+2 yl=l//B yr=r//B if yl==yr: g+=sum(stls1[l:r+1]) else: g+=sum(stls1[l:yl*B+B]) g+=sum(stls2[yl+1:yr]) g+=sum(stls1[yr*B:r+1]) ls[i]=g stls1[d[v]]+=v stls2[d[v]//B]+=v i=n-1-p v=a[i] g=0 l=0 r=d[v]-1 yl=l//B yr=r//B if yl==yr: g+=sum(strc1[l:r+1]) else: g+=sum(strc1[l:yl*B+B]) g+=sum(strc2[yl+1:yr]) g+=sum(strc1[yr*B:r+1]) rc[i]=g strc1[d[v]]+=1 strc2[d[v]//B]+=1 g=0 l=0 r=d[v]-1 yl=l//B yr=r//B if yl==yr: g+=sum(strs1[l:r+1]) else: g+=sum(strs1[l:yl*B+B]) g+=sum(strs2[yl+1:yr]) g+=sum(strs1[yr*B:r+1]) rs[i]=g strs1[d[v]]+=v strs2[d[v]//B]+=v print(sum(ls[i]*rc[i]+lc[i]*a[i]*rc[i]+lc[i]*rs[i] for i in range(n))%M)