#include <bits/stdc++.h>
using namespace std;
#define int long long
inline int read() {
int x = 0, f = 0; char c = 0;
while (!isdigit(c)) f |= c == '-', c = getchar();
while (isdigit(c)) x = (x << 3) + (x << 1) + (c & 15), c = getchar();
return f ? -x : x;
}
#define N 100005
#define P 1000000007
int n, a[N], res, mn[N], mx[N], smn[N], smx[N], s[N];
void cdx(int l, int r) {
if (l == r) {
res = (res + a[l] * a[r] % P) % P;
return;
}
int mid = (l + r) >> 1;
cdx(l, mid), cdx(mid + 1, r);
mn[mid] = 1e9, mx[mid] = 0;
s[mid] = smx[mid] = smn[mid] = 0;
for (int i = mid + 1; i <= r; i ++) {
mn[i] = min(mn[i - 1], a[i]);
mx[i] = max(mx[i - 1], a[i]);
s[i] = (s[i - 1] + mn[i] * mx[i] % P) % P;
}
mn[mid] = 0;
for (int i = mid + 1; i <= r; i ++) {
smn[i] = (smn[i - 1] + mn[i]) % P;
smx[i] = (smx[i - 1] + mx[i]) % P;
}
for (int i = mid, j = mid, k = mid, lmx = 0, lmn = 1e9; i >= l; i --) {
lmx = max(lmx, a[i]), lmn = min(lmn, a[i]);
while (j < r && mx[j + 1] <= lmx) j ++;
while (k < r && mn[k + 1] >= lmn) k ++;
(res += (s[r] - s[max(j, k)] + P) % P) %= P;
(res += lmx * lmn % P * max(0ll, min(j, k) - mid) % P) %= P;
if (j <= k) (res += lmn * ((smx[k] - smx[j] + P) % P) % P) %= P;
else (res += lmx * ((smn[j] - smn[k] + P) % P) % P) %= P;
}
for (int i = mid; i <= r; i ++) mn[i] = mx[i] = s[i] = 0;
}
signed main() {
freopen("simple.in", "r", stdin);
freopen("simple.out", "w", stdout);
n = read();
for (int i = 1; i <= n; i ++) {
a[i] = read();
}
cdx(1, n), printf("%lld\n", res);
return 0;
}