#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long n;
  cin >> n;
  long long h[n];
  for (long long i = 0; i < n; ++i) {
    cin >> h[i];
  }
  if (n == 1) {
    cout << h[0] - 1;
    exit(0);
  }
  long long r[n];
  r[0] = min(h[0], h[1]) - 1;
  for (long long j = 1; j < n - 1; ++j) {
    r[j] = min(min(h[j - 1], h[j + 1]), h[j]) - 1;
  }
  r[n - 1] = min(h[n - 1], h[n - 2]) - 1;
  long long cnt[n];
  cnt[n - 1] = r[n - 1];
  for (long long k = n - 2; k >= 1; --k) {
    cnt[k] = (cnt[k + 1] * r[k] + (min(h[k], h[k - 1]) - 1)) % mod;
  }
  long long ans = h[n - 1] - 1;
  for (long long l = n - 2; l >= 0; --l) {
    ans += (min(h[l], h[l + 1]) - 1) * cnt[l + 1] + h[l] - 1;
    ans %= mod;
  }
  cout << ans;
}
