// ayy // ' lamo // SUBLIME HAX #include using namespace std; template ostream &operator<<(ostream &os,const pair &x) { return os<<"("< struct is_iterable { template static long check(...); template static char check(int, typename T::const_iterator = C().end()); enum { value = sizeof(check(0)) == sizeof(char), neg_value = sizeof(check(0)) != sizeof(char) }; }; template ostream &_out_str(ostream &os,const T &x) { return os<<'"'< ostream &_dbg2_5(ostream &,const T &); template using eit=typename enable_if::type; template inline ostream &_dbg3(ostream &os,eit::neg_value,const T> &x) { return os< inline ostream &_dbg3(ostream &os,eit::value,const T> &V) { os<<"{"; bool ff=0; for(const auto &E:V) _dbg2_5(ff?os<<",":os,E), ff=1; return os<<"}"; } template<> inline ostream &_dbg3(ostream &os,const string &x) { return _out_str(os,x); } template<> inline ostream &_dbg3(ostream &os,const char *const &x) { return _out_str(os,x); } template inline ostream &_dbg2_5(ostream &os,const T &x) { return _dbg3(os,x); } template ostream &_dbg2(ostream &os,vector::iterator nm,const T &x,Args&&... args); inline ostream &_dbg2(ostream &os,vector::iterator) { return os; } template inline ostream &_dbg2(ostream &os,vector::iterator nm,const char *x,Args&&... args) { return _dbg2(_dbg3(os<<" ",x),nm+1,args...); } template inline ostream &_dbg2(ostream &os,vector::iterator nm,const T &x,Args&&... args) { return _dbg2(_dbg3(os<<" "<<*nm<<"=",x),nm+1,args...); } vector split(string s) { vector Z; string z=""; s+=','; int dep=0; for(char c:s) { if(c==',' && !dep) Z.push_back(z),z=""; else z+=c; if(c=='(') ++dep; if(c==')') --dep; } return Z; } template inline ostream &_dbg1(int ln,const string &nm,Args&&... args) { auto nms=split(nm); return _dbg2(cerr<<"L"< // using namespace __gnu_pbds; typedef unsigned long long ull; typedef long long ll; typedef long double ld; //CARE typedef complex pt; const ld eps=(ld)1e-8; const ld tau=2*(ld)acosl(-1); const int inf=1e9+99; const ll linf=1e18+99; const int P=1e9+7; #define ll __int128 ll ONE=1; const int N=256<<10; int n,a[N+N]; pair mx[N+N+N+N]; pair Q(int L,int R) { assert(L<=R); L+=N+N; R+=N+N; pair Z={-1,-1}; for(;;) { if(L&1) Z=max(Z,mx[L++]); if(!(R&1)) Z=max(Z,mx[R--]); if(L>R) return assert(Z.fi>0 && Z.se>0),Z; L>>=1,R>>=1; } } ll _g(int a,int b) { // dbg(a,b,(ONE*a*b*(a+b+2)/2%P)-(ONE*a*b%P)); return (ONE*a*b*(a+b+2)/2%P)-(ONE*a*b%P); } ll g(int L,int R) { if(L>R) return 0; pair p=Q(L,R); int M=p.se; int Z=p.fi; return g(L,M-1)+g(M+1,R)+ONE*Z*_g(M-L+1,R-M+1)%P; } ll ssq_ll(int a) { return ONE*a*(a+1)*(2*a+1)/6; } ll ssq(int a) { return ssq_ll(a)%P; } ll _h(int a,int b,int c,bool rev) { rev=!rev; ++c, --b; int L1=b; int R1=rev ? c-1 : c+1; int L2=R1+1; int R2=b+a; if(R1>R2) R1=R2, L2=R2+1; if(L2R) return 0; if(R<=n || L>n) return 0; pair p=Q(L,R); int M=p.se; int Z=p.fi; if(M<=n) return h(M+1,R)+ONE*Z*_h(M-L,n+1-M+1,R-(n+1),false)%P; else return h(L,M-1)+ONE*Z*_h(R-M,M-n+1,n-L,true)%P; } int32_t main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",a+i), a[n+i]=a[i]; for(int i=N+N;--i>=0;) mx[i+N+N]={a[i],i}; for(int i=N+N;--i>=0;) mx[i]=max(mx[i+i],mx[i+i+1]); ll diff_ct = ONE*n*(n+1)/2; if(diff_ct & 1) diff_ct = (diff_ct)%P*((diff_ct+1)/2)%P; else diff_ct = (diff_ct+1)%P*(diff_ct/2)%P; for(int l=1;l<=n;l++) { diff_ct -= ONE*l*(l+1)/2%P; } for(int l=1;l