#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
long long arr[1000003];
long long dp[1000003][2];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%lld", &arr[i]);
  arr[n + 1] = 2e9;
  if (arr[1] >= arr[2]) {
    dp[1][1] = arr[2] - 1;
    dp[1][0] = arr[1] - arr[2];
  } else
    dp[1][1] = arr[1] - 1;
  for (int i = 2; i <= n; i++) {
    if (arr[i] >= arr[i + 1]) {
      dp[i][1] += arr[i + 1] - 1;
      dp[i][0] += arr[i] - arr[i + 1];
      if (arr[i - 1] >= arr[i]) {
        dp[i][1] += (dp[i - 1][1] * (arr[i + 1] - 1)) % MOD;
        dp[i][1] %= MOD;
        dp[i][0] += (dp[i - 1][1] * (arr[i] - arr[i + 1])) % MOD;
        dp[i][0] %= MOD;
      } else {
        dp[i][1] += (dp[i - 1][1] * (min(arr[i - 1], arr[i + 1]) - 1)) % MOD;
        dp[i][1] %= MOD;
        dp[i][0] +=
            (dp[i - 1][1] * (max(arr[i + 1], arr[i - 1]) - arr[i + 1])) % MOD;
        dp[i][0] %= MOD;
      }
    } else {
      dp[i][1] += arr[i] - 1;
      dp[i][1] %= MOD;
      dp[i][1] += (dp[i - 1][1] * (min(arr[i], arr[i - 1]) - 1)) % MOD;
      dp[i][1] %= MOD;
    }
  }
  long long res = 0;
  for (int i = 1; i <= n; i++) {
    res += dp[i][0] + dp[i][1];
    res %= MOD;
  }
  res = ((res % MOD) + MOD) % MOD;
  printf("%lld\n", res);
}
