#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int MOD = 1e9 + 7;
const int N = 1e6 + 10;
int n, a[N];
long long f[N];
void prepare() {
  scanf("%d", &n);
  for (auto i = (1); i <= (n); i++) scanf("%d", a + i);
  for (auto i = (1); i <= (n); i++) a[i]--;
}
long long solve() {
  long long ans = 0;
  for (auto st = (1); st <= (n); st++) {
    int peak = st;
    while (peak < n && a[peak + 1] >= a[peak]) peak++;
    int en = peak;
    while (en < n && a[en + 1] <= a[en]) en++;
    for (auto i = (st); i <= (peak); i++) {
      f[i] = (f[i - 1] * a[i - 1] + a[i]) % MOD;
      ans = (ans + f[i]) % MOD;
    }
    if (en > peak) {
      for (auto i = (en); i >= (peak); i--) {
        f[i] = (f[i + 1] * a[i + 1] + a[i]) % MOD;
        ans = (ans + f[i]) % MOD;
      }
      ans = (ans - a[peak] + MOD) % MOD;
      ans = (ans +
             f[peak - 1] * f[peak + 1] % MOD * min(a[peak - 1], a[peak + 1])) %
            MOD;
      long long force = a[en];
      f[en] = a[en];
      for (auto i = (en - 1); i >= (peak + 1); i--) {
        force = force * a[i + 1] % MOD;
        f[en] = (f[en] + force) % MOD;
      }
      f[en] = (f[en] + force * a[peak + 1]) % MOD;
      force = force * min(a[peak - 1], a[peak + 1]) % MOD;
      f[en] = (f[en] + force * f[peak - 1]) % MOD;
    }
    st = en;
  }
  return ans;
}
int main() {
  prepare();
  cout << solve();
}
