#include <bits/stdc++.h>   using namespace std;   inline void splay(long long &v) {     v = 0;     char c = 0;     int p = 1;     while (c <  0  || c >  9 ) {       if (c ==  - ) p = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       v = (v << 3) + (v << 1) + c -  0 ;       c = getchar();     }     v *= p;   }   long long a[1000010];   long long dp[1000010], ans;   int main() {     long long n;     splay(n);     for (int i = 1; i <= n; i++) {       splay(a[i]);       a[i]--;     }     dp[1] = min(a[1], a[2]);     ans += a[1];     for (int i = 2; i <= n; i++) {       ans += a[i];       long long x = min(a[i], a[i - 1]);       long long y = min(x, a[i + 1]), z = min(a[i], a[i + 1]);       ans += dp[i - 1] * x;       dp[i] = dp[i - 1] * y + z;       ans %= 1000000007LL;       dp[i] %= 1000000007LL;     }     cout << ans << endl;   }   