#include <bits/stdc++.h> using namespace std; typedef pair<int,int> PII; const int kMod = 1000000007; long F(long t) { if (t < 1) return 0; return t*(t+1)*(t+2)/6; } long H(long n, long k) { return k*n*(n+k)/2; } long H1(long n, long k, long m) { return (k*n*(n+k) + k*m*(m-1))/2 - F(m-1-n) + F(m-1-n-k); } long H2(long n, long k, long m) { return (k*n*(n+k) + n*m*(m+1))/2 - F(m+1-k) + F(m+1-n-k); } int solve(const vector<int>& A) { const int N = A.size(); int max_value = 0; for(int i=0; i<N; ++i) { max_value = max(max_value, A[i]); } int first_max, last_max; for(int i=0; i<N; ++i) { if (max_value == A[i]) { first_max = i; break; } } for(int i=N-1; i>=0; --i) { if (max_value == A[i]) { last_max = i; break; } } __int128_t nonmax_count = 0; long res = 0; vector<long> ks(N, 0L), ns(N, 0L), ms(N, 0L); vector<PII> leb, reb; leb.reserve(N); reb.reserve(N); // left end leb.push_back(PII(max_value, last_max)); for(int i=last_max+1; i<N; ++i) { int cur_A = A[i]; while(leb.back().first < cur_A) leb.pop_back(); if (leb.back().first == cur_A) { leb.back().second = i; } else { leb.push_back(PII(cur_A, i)); } } for(int i=0; i<first_max; ++i) { int cur_A = A[i]; while(leb.back().first < cur_A) leb.pop_back(); if (leb.back().second >= last_max) { ks[i] = i+1; ms[i] = N-1 - leb.back().second; } else { ks[i] = i - leb.back().second; } if (leb.back().first == cur_A) { leb.back().second = i; } else { leb.push_back(PII(cur_A, i)); } } reb.push_back(PII(max_value, first_max)); for(int i=first_max-1; i>=0; --i) { int cur_A = A[i]; while(reb.back().first <= cur_A) { reb.pop_back(); } ns[i] = reb.back().second - i; reb.push_back(PII(cur_A, i)); } for(int i=0; i<first_max; ++i) { int cur_A = A[i]; long cur_count = H2(ns[i], ks[i], ms[i]); nonmax_count += cur_count; res = (res + cur_count%kMod*A[i]%kMod)%kMod; } // middle leb.clear(); leb.push_back(PII(max_value, first_max)); for(int i=first_max+1; i<last_max; ++i) { int cur_A = A[i]; while(leb.back().first < cur_A) { leb.pop_back(); } if (cur_A != max_value) { ks[i] = i - leb.back().second; } if (leb.back().first == cur_A) { leb.back().second = i; } else { leb.push_back(PII(cur_A, i)); } } reb.clear(); reb.push_back(PII(max_value, last_max)); for(int i=last_max-1; i>first_max; --i) { int cur_A = A[i]; while(reb.size() && (reb.back().first <= cur_A)) { reb.pop_back(); } if (cur_A != max_value) { ns[i] = reb.back().second - i; } reb.push_back(PII(cur_A, i)); } for(int i=first_max+1; i<last_max; ++i) { if (ns[i]) { long cur_count = H(ns[i], ks[i]); nonmax_count += cur_count; res = (res + cur_count%kMod*A[i]%kMod)%kMod; } } // right end leb.clear(); leb.push_back(PII(max_value, last_max)); for(int i=last_max+1; i<N; ++i) { int cur_A = A[i]; while(leb.back().first < cur_A) { leb.pop_back(); } ks[i] = i - leb.back().second; if (leb.back().first == cur_A) { leb.back().second = i; } else { leb.push_back(PII(cur_A, i)); } } reb.clear(); reb.push_back(PII(max_value, first_max)); for(int i=first_max-1; i>=0; --i) { int cur_A = A[i]; while(reb.back().first <= cur_A) reb.pop_back(); reb.push_back(PII(cur_A, i)); } for(int i=N-1; i>last_max; --i) { int cur_A = A[i]; while(reb.back().first <= cur_A) {reb.pop_back(); } if (reb.back().second <= first_max) { ns[i] = N-i; ms[i] = reb.back().second; } else { ns[i] = reb.back().second - i; } reb.push_back(PII(cur_A, i)); } for(int i=last_max+1; i<N; ++i) { long cur_count = H1(ns[i], ks[i], ms[i]); nonmax_count += cur_count; res = (res + cur_count%kMod*A[i]%kMod)%kMod; } long NN = long(N+1)*N/2; __int128_t NNN = __int128_t(NN+1)*NN/2; __int128_t max_count = NNN - nonmax_count; res = (res + max_count%kMod*max_value%kMod)%kMod; return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int N; cin >> N; vector<int> A(N); for(int i=0; i<N; ++i) { cin >> A[i]; } cout << solve(A) << endl; return 0; }