// ayy
// ' lamo
// SUBLIME HAX
	#include <bits/stdc++.h>
	using namespace std;
	template<class T,class U>
	ostream &operator<<(ostream &os,const pair<T,U> &x) {
		return os<<"("<<x.first<<","<<x.second<<")";
	}
	namespace dbg_ns {
		template<typename C>
		struct is_iterable {
			template<class T> static long check(...);
			template<class T> static char check(int,
				typename T::const_iterator = C().end());
			enum {
				value = sizeof(check<C>(0)) == sizeof(char),
				neg_value = sizeof(check<C>(0)) != sizeof(char)
			};
		};
		template<class T> ostream &_out_str(ostream &os,const T &x) {
			return os<<'"'<<x<<'"';
		}
		template<class T> ostream &_dbg2_5(ostream &,const T &);
		template<bool B,typename T=void> using eit=typename enable_if<B,T>::type;
		template<class T>
		inline ostream &_dbg3(ostream &os,eit<is_iterable<T>::neg_value,const T> &x) {
			return os<<x;
		}
		template<class T>
		inline ostream &_dbg3(ostream &os,eit<is_iterable<T>::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<string>(ostream &os,const string &x) {
			return _out_str(os,x);
		}
		template<>
		inline ostream &_dbg3<const char *>(ostream &os,const char *const &x) {
			return _out_str(os,x);
		}
		template<class T> inline ostream &_dbg2_5(ostream &os,const T &x) {
			return _dbg3<T>(os,x);
		}
		template<class T,typename... Args> ostream &_dbg2(ostream &os,vector<string>::iterator nm,const T &x,Args&&... args);
		inline ostream &_dbg2(ostream &os,vector<string>::iterator) { return os; }
		template<typename... Args>
		inline ostream &_dbg2(ostream &os,vector<string>::iterator nm,const char *x,Args&&... args) {
			return _dbg2(_dbg3<const char *>(os<<"  ",x),nm+1,args...);
		}
		template<class T,typename... Args>
		inline ostream &_dbg2(ostream &os,vector<string>::iterator nm,const T &x,Args&&... args) {
			return _dbg2(_dbg3<T>(os<<"  "<<*nm<<"=",x),nm+1,args...);
		}
		vector<string> split(string s) {
			vector<string> 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<typename... Args> inline ostream &_dbg1(int ln,const string &nm,Args&&... args) {
			auto nms=split(nm);
			return _dbg2(cerr<<"L"<<ln<<":",nms.begin(),args...)<<endl;
		}
	}
	#define dbg(...) dbg_ns::_dbg1(__LINE__,#__VA_ARGS__,__VA_ARGS__)
	#define sz(x) (int(x.size()))
	#define eprintf(...) fprintf(stderr,__VA_ARGS__)
	#define fi first
	#define se second
	#define pb push_back
// END SUBLIME HAX
// #include <bits/extc++.h>
// using namespace __gnu_pbds;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld; //CARE
typedef complex<ld> 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<int,int> mx[N+N+N+N];

pair<int,int> Q(int L,int R) {
	assert(L<=R);
	L+=N+N;
	R+=N+N;
	pair<int,int> 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<int,int> 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(L2<L1) L2=L1, R1=L1-1;
	if(!rev) --L1,--R1;
	ll Z1=0;
	Z1+=ONE*(2*c+1)*R1*(R1+1)/2;
	Z1-=ONE*(2*c+1)*(L1-1)*L1/2;
	Z1-=ssq_ll(R1);
	Z1+=ssq_ll(L1-1);
	assert(!(Z1%2)); Z1/=2;
	if(rev) Z1-=ONE*R1*(R1+1)/2, Z1+=ONE*(L1-1)*L1/2;
	if(rev) --c;
	ll Z2=ONE*c*(c+1)/2%P*(R2-L2+1);
	// dbg(a,b,c,rev,Z1,Z2,(Z1+Z2)%P);
	return (Z1+Z2)%P;
}
ll h(int L,int R) {
	if(L>R) return 0;
	if(R<=n || L>n) return 0;
	pair<int,int> 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<n;l++) {
		diff_ct -= ONE*l*(l+1)%P;
	}
	diff_ct %= P;
	ll Z = diff_ct * (*max_element(a+1,a+n+1)) % P;
	// dbg(diff_ct,Z);

	Z += g(1,n); Z%=P;
	Z += h(1,n+n); Z%=P;
	if(Z<0) Z+=P;
	printf("%d\n",int(Z));
}