#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);
}

### Prompt

Create a solution in cpp for the following problem:

Long ago, Vasily built a good fence at his country house. Vasily calls a fence good, if it is a series of n consecutively fastened vertical boards of centimeter width, the height of each in centimeters is a positive integer. The house owner remembers that the height of the i-th board to the left is hi.

Today Vasily decided to change the design of the fence he had built, by cutting his top connected part so that the fence remained good. The cut part should consist of only the upper parts of the boards, while the adjacent parts must be interconnected (share a non-zero length before cutting out of the fence).

You, as Vasily's curious neighbor, will count the number of possible ways to cut exactly one part as is described above. Two ways to cut a part are called distinct, if for the remaining fences there is such i, that the height of the i-th boards vary.

As Vasily's fence can be very high and long, get the remainder after dividing the required number of ways by 1 000 000 007 (109 + 7).

Input

The first line contains integer n (1 ≤ n ≤ 1 000 000) — the number of boards in Vasily's fence.

The second line contains n space-separated numbers h1, h2, ..., hn (1 ≤ hi ≤ 109), where hi equals the height of the i-th board to the left.

Output

Print the remainder after dividing r by 1 000 000 007, where r is the number of ways to cut exactly one connected part so that the part consisted of the upper parts of the boards and the remaining fence was good.

Examples

Input

2
1 1


Output

0


Input

3
3 4 2


Output

13

Note

From the fence from the first example it is impossible to cut exactly one piece so as the remaining fence was good.

All the possible variants of the resulting fence from the second sample look as follows (the grey shows the cut out part): 

<image>

### Response

```cpp
#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);
}

```