#include using namespace std; using Int = long long; template inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template inline void chmax(T1 &a,T2 b){if(a struct Mint{ static constexpr T mod = MOD; T v; Mint():v(0){} Mint(signed v):v(v){} Mint(long long t){v=t%MOD;if(v<0) v+=MOD;} Mint pow(long long k){ Mint res(1),tmp(v); while(k){ if(k&1) res*=tmp; tmp*=tmp; k>>=1; } return res; } static Mint add_identity(){return Mint(0);} static Mint mul_identity(){return Mint(1);} Mint inv(){return pow(MOD-2);} Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;} Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;} Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;} Mint& operator/=(Mint a){return (*this)*=a.inv();} Mint operator+(Mint a) const{return Mint(v)+=a;} Mint operator-(Mint a) const{return Mint(v)-=a;} Mint operator*(Mint a) const{return Mint(v)*=a;} Mint operator/(Mint a) const{return Mint(v)/=a;} Mint operator-() const{return v?Mint(MOD-v):Mint(v);} bool operator==(const Mint a)const{return v==a.v;} bool operator!=(const Mint a)const{return v!=a.v;} bool operator <(const Mint a)const{return v constexpr T Mint::mod; template ostream& operator<<(ostream &os,Mint m){os< struct SegmentTree{ using F = function; using G = function; using H = function; Int n,height; F f; G g; H h; T ti; E ei; vector dat; vector laz; SegmentTree(F f,G g,H h,T ti,E ei): f(f),g(g),h(h),ti(ti),ei(ei){} void init(Int n_){ n=1;height=0; while(n &v){ Int n_=v.size(); init(n_); for(Int i=0;i>i); } inline void recalc(Int k){ while(k>>=1) dat[k]=f(reflect((k<<1)|0),reflect((k<<1)|1)); } void update(Int a,Int b,E x){ if(a>=b) return; thrust(a+=n); thrust(b+=n-1); for(Int l=a,r=b+1;l>=1,r>>=1){ if(l&1) laz[l]=h(laz[l],x),l++; if(r&1) --r,laz[r]=h(laz[r],x); } recalc(a); recalc(b); } void set_val(Int a,T x){ thrust(a+=n); dat[a]=x;laz[a]=ei; recalc(a); } T query(Int a,Int b){ if(a>=b) return ti; thrust(a+=n); thrust(b+=n-1); T vl=ti,vr=ti; for(Int l=a,r=b+1;l>=1,r>>=1) { if(l&1) vl=f(vl,reflect(l++)); if(r&1) vr=f(reflect(--r),vr); } return f(vl,vr); } template Int find(Int st,C &check,T &acc,Int k,Int l,Int r){ if(l+1==r){ acc=f(acc,reflect(k)); return check(acc)?k-n:-1; } propagate(k); Int m=(l+r)>>1; if(m<=st) return find(st,check,acc,(k<<1)|1,m,r); if(st<=l&&!check(f(acc,dat[k]))){ acc=f(acc,dat[k]); return -1; } Int vl=find(st,check,acc,(k<<1)|0,l,m); if(~vl) return vl; return find(st,check,acc,(k<<1)|1,m,r); } template Int find(Int st,C &check){ T acc=ti; return find(st,check,acc,1,0,n); } }; //INSERT ABOVE HERE signed main(){ Int n; cin>>n; vector as(n); for(Int i=0;i>as[i]; const Int INF = 1e15; vector vs(n,INF); vector ps(n); for(Int i=0;i; auto f=[&](M a,M b){return a+b;}; auto g=[&](M ,M b){return b;}; M ti(0); M ei; ei.v=-1; SegmentTree seg(f,g,g,ti,ei); seg.build(vector(n,ti)); Int m=*max_element(ps.begin(),ps.end()); vector< vector > G(m+1); for(Int i=0;i dp(n); for(Int i=0;i; vector

vp; for(Int i:G[t]) vp.emplace_back(as[i],n+i); for(Int i:G[t+1]) vp.emplace_back(as[i],i); sort(vp.begin(),vp.end()); for(auto p:vp){ Int k=p.second%n; if(ps[k]==t) seg.set_val(k,dp[k]); else dp[k]=seg.query(0,k); } } M ans{0}; for(Int i=0;i