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)] tl=1<<20 def update(p,x): while p0: a+=st[p] p-=p&(-p) return a st=[0]*tl for v,i in q[::-1]: update(i+1,1) ok=i ng=-1 while abs(ok-ng)>1: m=(ok+ng)//2 l=m r=i if getsum(r+1)-getsum(l)==1: ok=m else: ng=m L=ok ok=i ng=n while abs(ok-ng)>1: m=(ok+ng)//2 l=i r=m if getsum(r+1)-getsum(l)==1: ok=m else: ng=m R=ok p[i]+=[(L,i,v)] p[R+1]+=[(L,i,pow(v,M-2,M))] st=[0]*tl for v,i in q: update(i+1,1) ok=i ng=-1 while abs(ok-ng)>1: m=(ok+ng)//2 l=m r=i if getsum(r+1)-getsum(l)==1: ok=m else: ng=m L=ok ok=i ng=n while abs(ok-ng)>1: m=(ok+ng)//2 l=i r=m if getsum(r+1)-getsum(l)==1: ok=m else: ng=m R=ok p[i]+=[(L,i,v)] p[R+1]+=[(L,i,pow(v,M-2,M))] exit() ans=0 B=448 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)