#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void amin(T &x, U y) {
  if (y < x) x = y;
}
template <typename T, typename U>
inline void amax(T &x, U y) {
  if (x < y) x = y;
}
template <int MOD>
struct ModInt {
  static const int Mod = MOD;
  unsigned x;
  ModInt() : x(0) {}
  ModInt(signed sig) {
    int sigt = sig % MOD;
    if (sigt < 0) sigt += MOD;
    x = sigt;
  }
  ModInt(signed long long sig) {
    int sigt = sig % MOD;
    if (sigt < 0) sigt += MOD;
    x = sigt;
  }
  int get() const { return (int)x; }
  ModInt &operator+=(ModInt that) {
    if ((x += that.x) >= MOD) x -= MOD;
    return *this;
  }
  ModInt &operator-=(ModInt that) {
    if ((x += MOD - that.x) >= MOD) x -= MOD;
    return *this;
  }
  ModInt &operator*=(ModInt that) {
    x = (unsigned long long)x * that.x % MOD;
    return *this;
  }
  ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
  ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
  ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
};
vector<int> h;
int calcL(int i) {
  assert(i + 1 < (int)h.size());
  return h[i] - (h[i] < h[i + 1] ? 1 : h[i] - h[i + 1] + 1);
}
int calcR(int i) {
  assert(i > 0);
  return h[i] - (h[i] < h[i - 1] ? 1 : h[i] - h[i - 1] + 1);
}
int calcLR(int i) {
  if (i == 0)
    return calcL(i);
  else if (i + 1 == h.size())
    return calcR(i);
  else
    return min(calcL(i), calcR(i));
}
ModInt<1000000007> rec(int i, int j) {
  if (i == j) return ModInt<1000000007>();
  int mid = i + (j - i) / 2;
  ModInt<1000000007> r;
  r += h[mid] - 1;
  ModInt<1000000007> sumL, sumR;
  if (i < mid) {
    ModInt<1000000007> prod = 1, mul = calcR(mid);
    for (int k = mid - 1; k >= i; --k) {
      ModInt<1000000007> cnt = prod * calcL(k);
      sumL += cnt;
      r += cnt * mul;
      prod *= calcLR(k);
    }
  }
  if (mid < j - 1) {
    ModInt<1000000007> prod = 1, mul = calcL(mid);
    for (int k = mid + 1; k < j; ++k) {
      ModInt<1000000007> cnt = prod * calcR(k);
      sumR += cnt;
      r += cnt * mul;
      prod *= calcLR(k);
    }
  }
  if (i < mid && mid < j - 1) {
    ModInt<1000000007> mul = calcLR(mid);
    r += sumL * sumR * mul;
  }
  r += rec(i, mid);
  r += rec(mid + 1, j);
  return r;
}
int main() {
  int n;
  while (~scanf("%d", &n)) {
    h.resize(n);
    for (int i = 0; i < n; ++i) scanf("%d", &h[i]);
    ModInt<1000000007> ans = rec(0, n);
    printf("%d\n", ans.get());
  }
  return 0;
}
