#include <bits/stdc++.h>
using namespace std;
int f[1000005][2], h[1000005], ans, n, m;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", h + i);
  h[n + 1] = 2000000000;
  for (int i = 1; i <= n; i++) {
    int mjy = min(h[i - 1], h[i]), mzl = min(h[i], h[i + 1]);
    if (mjy <= mzl)
      (f[i][1] += 1LL * f[i - 1][1] * (mjy - 1) % 1000000007) %= 1000000007;
    else {
      (f[i][1] += 1LL * f[i - 1][1] * (mzl - 1) % 1000000007) %= 1000000007;
      (f[i][0] += 1LL * f[i - 1][1] * (mjy - mzl) % 1000000007) %= 1000000007;
    }
    (f[i][0] += h[i] - mzl) %= 1000000007;
    (f[i][1] += mzl - 1) %= 1000000007;
    (ans += (f[i][0] + f[i][1]) % 1000000007) %= 1000000007;
  }
  printf("%d\n", ans);
  return 0;
}
