#include <bits/stdc++.h>
using namespace std;
int n, a[1000010], f[1000010], ans;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  a[n + 1] = 2e9 + 1;
  for (int i = 1; i <= n; i++) {
    int x = min(a[i - 1], a[i]), y = min(a[i], a[i + 1]);
    if (x <= y)
      f[i] = (long long)f[i - 1] * (x - 1) % 1000000007;
    else {
      f[i] = (long long)f[i - 1] * (y - 1) % 1000000007;
      (ans += (long long)f[i - 1] * (x - y) % 1000000007) %= 1000000007;
    }
    (f[i] += y - 1) %= 1000000007;
    (ans += (a[i] - y + f[i]) % 1000000007) %= 1000000007;
  }
  printf("%d\n", (ans + 1000000007) % 1000000007);
  return 0;
}
