#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
const int MOD = 1e9 + 7;
const int INF = 1e8;
const int N = 1e6 + 7;
int n;
int h[N];
int f[N], g[N];
long long s[N];
inline long long sqr(long long x) { return x * x % MOD; }
int main() {
  scanf("%d", &n);
  for (int i = int(1); i <= int(n); ++i) scanf("%d", h + i);
  long long ans = 0;
  for (int i = int(1); i <= int(n - 1); ++i) f[i] = min(h[i], h[i + 1]) - 1;
  for (int i = int(2); i <= int(n - 1); ++i)
    g[i] = min(h[i - 1], min(h[i], h[i + 1])) - 1;
  for (int i = int(1); i <= int(n); ++i) ans += h[i] - 1, ans %= MOD;
  s[1] = f[1];
  for (int i = int(2); i <= int(n); ++i) {
    ans += s[i - 1] * f[i - 1] % MOD;
    ans %= MOD;
    s[i] = (s[i - 1] * g[i] % MOD + f[i]) % MOD;
  }
  cout << ans << endl;
  return 0;
}
