Problem:
Score: 500 points

Problem Statement

Given is a permutation P of \{1, 2, \ldots, N\}.

For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest value among P_L, P_{L+1}, \ldots, P_R.

Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.

Constraints

2 \le N \le 10^5

1 \le P_i \le N

P_i \neq P_j (i \neq j)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 \ldots P_N

Output

Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.

Sample Input 1

3
2 3 1

Sample Output 1

5

X_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.

Sample Input 2

5
1 2 3 4 5

Sample Output 2

30

Sample Input 3

8
8 2 7 3 4 5 6 1

Sample Output 3

136

Solution in Python (3.4.3) (judge verdict: Runtime Error)

//#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast,no-stack-protector") 
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>


#define ll long long
#define ull unsigned long long
#define int ll
#define ld long double
#define rep(i, l, r) for (int i = l; i < r; i++)
#define repb(i, r, l) for (int i = r; i > l; i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define ret(a) {cout << a; exit(0);}

using namespace std;

mt19937 mrand(random_device{}()); 
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b,ll mod) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

typedef pair < int , int > pii ;
typedef pair < long long  , long long > pll ;

const int N = 1e5 + 1 , LOG = 18;
int n ;
int a[N], up[N] , sp[LOG][N];
int get(int l , int r){
	int x = up[r - l + 1];
//	cout << l << ' ' << r << ' ' << max(sp[x][l] , sp[x][r - (1 << x) + 1]) << '\n';
	//0 
	return max(sp[x][l] , sp[x][r - (1 << x) + 1]);
}
main(){
	cin >> n;
	up[0] = -1;
	for(int i = 1;  i <= n; i++){
		up[i] = up[i / 2] + 1;
    	cin >> a[i];
		sp[0][i] = a[i];
	}	
	for(int j = 1 ; j < LOG; j++)
		for(int i = 1 ; i + (1 << j) - 1<= n; i++)
			sp[j][i] = max(sp[j - 1][i] , sp[j - 1][i + (1 << j - 1)]);
	ll ans = 0;
	for(int i = 1 ; i <= n; i++){
		if(a[i] == n)
			continue;
		int L = 0, R = 0;
		{
			int l = 1 , r = i;
			while(l <= r){
			 	int m = (l + r) >> 1;
			 	if(get(m,i) == a[i])
			 		L = m , r = m - 1;
			 	else l = m + 1;
			}
		}
		{
			int l = i , r = n;
			while(l <= r){
				int m = (l + r) >> 1;
				if(get(i , m) == a[i])	
					R = m , l = m + 1;
				else r = m - 1;
			}
		}

        if(get(1,i) > a[i]){
			int Mx = 0, MxL = 0;
			int l = 1 , r = i - 1;
			while(l <= r){
				int m = (l + r) >> 1;
				if(get(m , i) > a[i])
					Mx = m , l = m + 1;
				else r = m - 1;	
			}
			l = 1 , r = Mx - 1 , MxL = Mx;
			while(l <= r){
				int m = (l + r) >> 1;
				if(get(m , Mx - 1) < a[i])
					MxL = m , r = m - 1;
				else l = m + 1;
			}
			ans += a[i] * (Mx - MxL + 1) * (R - i + 1);
		}
		if(get(i,n) > a[i]){
			int Mx = 0, MxR = 0;
			int l = i + 1 , r = n;
			while(l <= r){
				int m = (l + r) >> 1;
				if(get(i , m) > a[i])
					Mx = m , r = m - 1;
				else l = m + 1;	
			}
			l = Mx + 1 , r = n , MxR = Mx;
			while(l <= r){
				int m = (l + r) >> 1;
				if(get(Mx + 1 , m) < a[i])
					MxR = m , l = m + 1;
				else r = m - 1;
			}
			ans += a[i] * (MxR - Mx + 1) * (i - L + 1);			
		}

	}		
	cout << ans;
}