#include using namespace std; const int mod = 1000000007; int maxim[4001][4001]; int v[400005]; int main() { #ifdef LOCAL freopen("a.in", "r", stdin); freopen("a.out", "w", stdout); #endif cin.tie(0); ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 1; i <= n; ++i) cin >> v[i]; if (n <= 4000) { for (int i = 1; i <= n; ++i) maxim[i][i] = v[i]; for (int k = 2; k <= n; ++k) { for (int i = 1, j = i + k - 1; j <= n; ++i, ++j) { maxim[i][j] = max(maxim[i + 1][j], maxim[i][j - 1]); } } vector vec; for (int k = 1; k <= n; ++k) { for (int i = 1, j = i + k - 1; j <= n; ++i, ++j) { vec.push_back(maxim[i][j]); } } stack st; vector dp(vec.size()); for (int i = 0; i < (int) vec.size(); ++i) { int x = vec[i]; while (!st.empty() && x >= vec[st.top()]) st.pop(); dp[i] = i - (st.empty() ? -1 : st.top()); st.push(i); } while (!st.empty()) st.pop(); for (int i = (int) vec.size() - 1; i >= 0; --i) { int x = vec[i]; while (!st.empty() && x > vec[st.top()]) st.pop(); dp[i] = 1LL * dp[i] * ((st.empty() ? (int) vec.size() : st.top()) - i) % mod; st.push(i); } int sol = 0; for (int i = 0; i < (int) vec.size(); ++i) sol = (sol + 1LL * dp[i] * vec[i]) % mod; cout << sol << endl; return 0; } stack< int > st; vector< int > lef(2 * n + 5), rig(2 * n + 5); for (int i = 1; i <= n; ++i) { while (!st.empty() && v[i] >= v[st.top()]) st.pop(); lef[i] = (st.empty() ? 0 : st.top()); st.push(i); } while (!st.empty()) st.pop(); for (int i = n; i; --i) { while (!st.empty() && v[i] > v[st.top()]) st.pop(); rig[i] = (st.empty() ? n + 1 : st.top()); st.push(i); } while (!st.empty()) st.pop(); long long sol = 0; for (int i = 1; i <= n; ++i) { int a = lef[i] + 1, b = rig[i] - 1; sol = sol + 1LL * v[i] * (i - a + 1) % mod * ( (1LL*(b+1)*(b+2) - 1LL*i*(i+1))/2 % mod ); sol %= mod; sol = sol - 1LL * (b - i + 1) * v[i] % mod * ( (1LL * i * (i + 1) - 1LL*a*(a-1)) / 2 % mod); sol %= mod; if (sol < 0) sol += mod; } int maxim = *max_element(v + 1, v + n + 1); long long sum = 0; for (int i = n-1; i > 1; --i) { sol = sol + 1LL * maxim * (sum + 1) % mod * (i - 1); sol %= mod; sum = (sum + i + 1) % mod; } if (n > 1) { sol = sol + 1LL * maxim * (sum + 1); sol %= mod; } sol = (sol + mod - 1LL*(n - 1)*maxim % mod) % mod; for (int i = n-1; i > 2; --i) { sol = sol + 1LL * maxim * i % mod * (i - 2) % mod; sol %= mod; } for (int i = n + 1; i < 2 * n; ++i) v[i] = max(v[i - n], v[i - n + 1]); for (int i = 1; i < 2 * n; ++i) { while (!st.empty() && v[i] >= v[st.top()]) st.pop(); lef[i] = (st.empty() ? 0 : st.top()); st.push(i); } while (!st.empty()) st.pop(); for (int i = 2 * n - 1; i; --i) { while (!st.empty() && v[i] > v[st.top()]) st.pop(); rig[i] = (st.empty() ? 2 * n : st.top()); st.push(i); } while (!st.empty()) st.pop(); for (int i = 1; i <= n; ++i) { if (rig[i] <= n + 1) continue; int a = lef[i]+1, b = rig[i]-1; int tm = min(n-i+1, b-n); if (1 <= tm) { sol = sol + 1LL * v[i] * (i-a+1) % mod * (1LL * tm * (tm+1) / 2 % mod); sol %= mod; } int tm2 = min(b - n, i-a+1+tm); if (tm + 1 <= tm2) { int aux = i-a+1+tm; sol += 1LL * v[i] * aux % mod * ((1LL * tm2 * (tm2+1) / 2 - 1LL * tm * (tm+1) / 2) % mod) % mod; sol %= mod; sol -= 1LL * v[i] * ((1LL * tm2 * (tm2 + 1) * (2*tm2 + 1) / 6 - 1LL * tm * (tm + 1) * (2*tm + 1) / 6) % mod) % mod - mod; sol %= mod; } tm = n - i; tm2 = min(n - a + 1, b - n); if (tm + 1 <= tm2) { int aux = b-n; sol += 1LL * v[i] * aux % mod * ((1LL * tm2 * (tm2+1) / 2 - 1LL * tm * (tm+1) / 2) % mod) % mod; sol %= mod; sol -= 1LL * v[i] * ((1LL * tm2 * (tm2 + 1) * (2*tm2 + 1) / 6 - 1LL * tm * (tm + 1) * (2*tm + 1) / 6) % mod) % mod - mod; sol %= mod; } } int nn = n; for (int j = n + 1; j < 2 * nn; ++j) { if (lef[j] >= nn) continue; int aa = lef[j]+1, bb = rig[j]-1; int a = n - (bb - n - 1); int b = n+1 + (n - aa); int i = n - (j - n - 1); int tm = min(n-i+1, b-n); if (1 <= tm) { sol = sol + 1LL * v[j] * (i-a+1) % mod * (1LL * tm * (tm+1) / 2 % mod); sol %= mod; } int tm2 = min(b - n, i-a+1+tm); if (tm + 1 <= tm2) { int aux = i-a+1+tm; sol += 1LL * v[j] * aux % mod * ((1LL * tm2 * (tm2+1) / 2 - 1LL * tm * (tm+1) / 2) % mod) % mod; sol %= mod; sol -= 1LL * v[j] * ((1LL * tm2 * (tm2 + 1) * (2*tm2 + 1) / 6 - 1LL * tm * (tm + 1) * (2*tm + 1) / 6) % mod) % mod - mod; sol %= mod; } tm = n-i; tm2 = min(n - a + 1, b - n); if (tm + 1 <= tm2) { int aux = b-n; sol += 1LL * v[j] * aux % mod * ((1LL * tm2 * (tm2+1) / 2 - 1LL * tm * (tm+1) / 2) % mod) % mod; sol %= mod; sol -= 1LL * v[j] * ((1LL * tm2 * (tm2 + 1) * (2*tm2 + 1) / 6 - 1LL * tm * (tm + 1) * (2*tm + 1) / 6) % mod) % mod - mod; sol %= mod; } } cout << sol << endl; return 0; } //Trust me, I'm the Doctor!