/*
*  Created by Andrei Arhire 03/02/2021
*  Copyright © 2021 Andrei Arhire. All rights reserved.
*  Expected verdict - Accepted
*/
#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define mp make_pair
using namespace std;
const   long long NR = 1e6 + 10 ;
const   long long oo = 1e9 + 100 ;
const   long long MOD = 1e9 + 7  ;
const   long long MOD2 = 1e9 + 9  ;

long long n ;
long long a [ NR ] ;
long long rmq [ 23 ][ NR ] ;
long long lg [ NR ] ;

long long query ( long long x , long long y )   {
   if ( x > y )    {
       swap ( x , y ) ;
   }
   return max ( rmq [ lg [ y - x + 1 ] ][ x ] , rmq [ lg [ y - x + 1 ] ][ y - ( 1 << ( lg [ y - x + 1 ] ) ) + 1 ] ) ;
}
long long ans , ans2 ;

long long lgp ( long long a , long long b )   {
   if ( !b )   return 1 ;
   if ( b % 2 )    return a * lgp ( a * a % MOD , b / 2 ) % MOD ;
   return lgp ( a * a % MOD , b / 2 ) ;
}
//ifstream in ("rectangles.in") ;
//ofstream out ("rectangles.out") ;
signed main () {
   long long j ;
   std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);
   long long i ;
   cin >> n ;
   for ( i = 1 ; i <= n ; ++ i )   {
       cin >> a [ i ] ;
       rmq [ 0 ][ i ] = a [ i ] ;
   }
   for ( i = 1 ; ( 1 << i ) <= n ; ++ i )   {
       for ( j = 1 ; j + ( 1 << i ) <= n + 1 ; ++ j )  {
           rmq [ i ][ j ] = max ( rmq [ i - 1 ][ j ] , rmq [ i - 1 ][ j + ( 1 << ( i - 1 ) ) ] ) ;
       }
   }
   for ( i = 2 ; i <= n ; ++ i )   {
       lg [ i ] = lg [ i >> 1 ] + 1 ;
   }

   for ( i = 1 ; i <= n ; ++ i )   {
       long long st , dr , sol , mid ;
       st = i + 1 ;
       dr = n ;
       sol = i ;
       while ( st <= dr )  {
           mid = ( st + dr ) / 2 ;
           if ( query( i + 1 , mid ) <= a [ i ] )  {
               sol = mid ;
               st = mid + 1 ;
           }   else    {
               dr = mid - 1 ;
           }
       }
       long long sol2 = i ;
       st = 1 ;
       dr = i - 1 ;
       while ( st <= dr )  {
           mid = ( st + dr ) / 2 ;
           if ( query( mid , i - 1 ) < a [ i ] )   {
               sol2 = mid ;
               dr = mid - 1 ;
           }   else    {
               st = mid + 1 ;
           }
       }
       long long sol3 = sol2 - 1 ;
       st = 1 ;
       dr = sol2 - 2 ;
       while ( st <= dr )  {
           mid = ( st + dr ) / 2 ;
           if ( query( mid , sol2 - 2 ) < a [ i ] )    {
               sol3 = mid ;
               dr = mid - 1 ;
           }   else    {
               st = mid + 1 ;
           }
       }
       long long sol4 = sol + 1 ;
       st = sol + 2 ;
       dr = n ;
       while ( st <= dr )  {
           mid = ( st + dr ) / 2 ;
           if ( query( sol + 2 , mid ) <= a [ i ] ) {
               sol4 = mid ;
               st = mid + 1 ;
           }   else    {
               dr = mid - 1 ;
           }
       }


       swap ( sol , sol2 ) ;
       if ( sol4 > n )
           sol4 = n ;
       if ( sol3 < 1 )
           sol3 = 1 ;

//            cout << sol << ' ' << sol2 << ' ' << sol3 << ' ' << sol4 << '\n' ;

               ans += ( ( ( sol4 - sol2 ) * ( i - sol + 1 ) % MOD ) * a [ i ] % MOD ) * a [ sol2 + 1 ] % MOD ;
 //              cout << ( ( ( sol4 - sol2 ) * ( i - sol + 1 ) % MOD ) * a [ i ] % MOD ) * a [ sol2 + 1 ] % MOD << '\n' ;

               ans += ( ( ( sol - sol3 ) * ( sol2 - i + 1 ) % MOD ) * a [ i ] % MOD ) * a [ sol - 1 ] % MOD ;
   //    cout << ( ( ( sol - sol3 ) * ( sol2 - i + 1 ) % MOD ) * a [ i ] % MOD ) * a [ sol - 1 ] % MOD  << '\n' ;

   }
   cout << ( ans ) % MOD << '\n' ;
   return 0 ;
}

