n=int(input()) a=list(map(int,input().split())) M=998244353 q=[(a[i],i) for i in range(n)] q.sort() p=[[] for i in range(n+1)] B=448 st1=[0]*B*B st2=[0]*B for v,i in q[::-1]: st1[i]+=1 st2[i//B]+=1 ok=i ng=-1 while abs(ok-ng)>1: m=(ok+ng)//2 g=0 l=m r=i 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]) if g==1: ok=m else: ng=m L=ok ok=i ng=n while abs(ok-ng)>1: m=(ok+ng)//2 g=0 l=i r=m 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]) if g==1: ok=m else: ng=m R=ok p[i]+=[(L,i,v)] p[R+1]+=[(L,i,pow(v,M-2,M))] st1=[0]*B*B st2=[0]*B for v,i in q: st1[i]+=1 st2[i//B]+=1 ok=i ng=-1 while abs(ok-ng)>1: m=(ok+ng)//2 g=0 l=m r=i 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]) if g==1: ok=m else: ng=m L=ok ok=i ng=n while abs(ok-ng)>1: m=(ok+ng)//2 g=0 l=i r=m 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]) if g==1: ok=m else: ng=m R=ok p[i]+=[(L,i,v)] p[R+1]+=[(L,i,pow(v,M-2,M))] ans=0 st1=[1]*B*B st2=[B]*B lt=[1]*B for y in range(n): for l,r,v in p[y]: yl=l//B yr=r//B for i in range(yl*B,yl*B+B): st1[i]*=lt[yl] st1[i]%=M lt[yl]=1 for i in range(yr*B,yr*B+B): st1[i]*=lt[yr] st1[i]%=M lt[yr]=1 if yl==yr: for i in range(l,r+1): st2[yl]-=st1[i] st1[i]*=v st1[i]%=M st2[yl]+=st1[i] st2[yl]%=M else: for i in range(l,yl*B+B): st2[yl]-=st1[i] st1[i]*=v st1[i]%=M st2[yl]+=st1[i] st2[yl]%=M for i in range(yl+1,yr): st2[i]*=v st2[i]%=M lt[i]=v for i in range(yr*B,r+1): st2[yr]-=st1[i] st1[i]*=v st1[i]%=M st2[yr]+=st1[i] st2[yr]%=M g=0 l=0 r=y yl=l//B yr=r//B for i in range(yl*B,yl*B+B): st1[i]*=lt[yl] st1[i]%=M lt[yl]=1 for i in range(yr*B,yr*B+B): st1[i]*=lt[yr] st1[i]%=M lt[yr]=1 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]) ans+=g ans%=M print(ans)