#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
const int MOD = 1000 * 1000 * 1000 + 7;
int a[N], n;
int ans;
int add(int x, int b) { return x + b - (x + b >= MOD) * MOD; }
int mul(int x, int b) { return (1LL * x * b) % MOD; }
void input() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  a[0] = a[n + 1] = 1;
}
void Run() {
  int nw[N];
  memset(nw, 0, sizeof nw);
  ans = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] > a[i + 1]) {
      ans = add(ans, a[i] - a[i + 1]);
      if (a[i - 1] > a[i + 1]) {
        ans = add(ans, mul(nw[i - 1], min(a[i], a[i - 1]) - a[i + 1]));
      }
    }
    nw[i] = add(nw[i], min(a[i], a[i + 1]) - 1);
    int lay = min(min(a[i], a[i + 1]), a[i - 1]) - 1;
    nw[i] = add(nw[i], mul(nw[i - 1], lay));
    ans = add(ans, nw[i]);
  }
  cout << ans << endl;
}
int main() {
  input();
  Run();
}
