#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int a[1 << 20], n;
long long var, dp, ans;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", a + i), a[i]--;
  dp = a[n];
  if (n > 1 && a[n] > a[n - 1]) var = a[n - 1];
  ans = dp;
  for (int i = n - 1; i > 0; i--) {
    long long prevdp = dp;
    if (a[i] < a[i + 1]) {
      dp = (1LL * a[i] + var * a[i]) % mod;
      if (i > 1 && a[i] > a[i - 1]) var = (a[i - 1] + a[i - 1] * var) % mod;
    } else {
      dp = (1LL * a[i] + prevdp * a[i + 1]) % mod;
      if (i > 1 && a[i] > a[i - 1])
        var = (a[i - 1] + min(a[i - 1], a[i + 1]) * prevdp) % mod;
    }
    ans = (ans + dp) % mod;
  }
  cout << ans;
}
