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=448 lc=[0]*n ls=[0]*n stc1=[0]*B*B stc2=[0]*B sts1=[0]*B*B sts2=[0]*B 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(stc1[l:r+1]) else: g+=sum(stc1[l:yl*B+B]) g+=sum(stc2[yl+1:yr]) g+=sum(stc1[yr*B:r+1]) lc[i]=g stc1[d[v]]+=1 stc2[d[v]//B]+=1 g=0 l=d[v]+1 r=n+2 yl=l//B yr=r//B if yl==yr: g+=sum(sts1[l:r+1]) else: g+=sum(sts1[l:yl*B+B]) g+=sum(sts2[yl+1:yr]) g+=sum(sts1[yr*B:r+1]) ls[i]=g sts1[d[v]]+=v sts2[d[v]//B]+=v rc=[0]*n rs=[0]*n stc1=[0]*B*B stc2=[0]*B sts1=[0]*B*B sts2=[0]*B 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(stc1[l:r+1]) else: g+=sum(stc1[l:yl*B+B]) g+=sum(stc2[yl+1:yr]) g+=sum(stc1[yr*B:r+1]) rc[i]=g stc1[d[v]]+=1 stc2[d[v]//B]+=1 g=0 l=0 r=d[v]-1 yl=l//B yr=r//B if yl==yr: g+=sum(sts1[l:r+1]) else: g+=sum(sts1[l:yl*B+B]) g+=sum(sts2[yl+1:yr]) g+=sum(sts1[yr*B:r+1]) rs[i]=g sts1[d[v]]+=v sts2[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)