#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 10;
inline int mu(int x, int y) { return 1LL * x * y % mod; }
inline int ad(int x, int y) { return (x + y) % mod; }
inline int wop(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1) res = mu(res, a);
    a = mu(a, a);
    b >>= 1;
  }
  return res;
}
inline int inv(int x) { return wop(x, mod - 2); }
int a[maxn], dp[2][maxn];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) scanf("%d", a + i), a[i]--;
  dp[0][0] = 1;
  dp[1][0] = a[0];
  dp[0][1] = ad(dp[1][0], dp[0][0]);
  dp[1][1] = ad(a[1], mu(min(a[0], a[1]), min(a[0], a[1])));
  for (int i = 2; i < n; i++) {
    dp[0][i] = ad(dp[0][i - 1], dp[1][i - 1]);
    if (!a[i - 1]) {
      dp[1][i] = a[i];
      continue;
    }
    if (!a[i]) continue;
    dp[1][i] = ad(mu(min(a[i - 1], a[i]), min(a[i - 1], a[i])), a[i]);
    if (!a[i - 2]) continue;
    int x = ad(dp[1][i - 1], mod - a[i - 1]);
    int y = mu(x, inv(min(a[i - 1], a[i - 2])));
    int t1 = min(a[i - 1], a[i]);
    int t2 = min(t1, a[i - 2]);
    int t3 = mu(t1, t2);
    dp[1][i] = ad(dp[1][i], mu(y, t3));
  }
  cout << ad(ad(dp[0][n - 1], dp[1][n - 1]), mod - 1) << endl;
}
