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 C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
#define rep(i, a, b) for (register ll i = (a); i <= (b); ++i)
#define per(i, a, b) for (register ll i = (a); i >= (b); --i)
#define loop(it, v) for (auto it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = edge[i].nex)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, cnt) memset(a, cnt, sizeof(a))
#define cop(a, b) memcpy(a, b, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define SC(t, x) static_cast <t> (x)
#define ub upper_bound
#define lb lower_bound
#define pqueue priority_queue
#define mp make_pair
#define pb push_back
#define pof pop_front
#define pob pop_back
#define fi first
#define se second
#define y1 y1_
#define Pi acos(-1.0)
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(x) freopen(x".in", "r", stdin),freopen(x".out", "w", stdout)
typedef double db ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef queue <pii> qii ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int maxn = 1e6 + 10 ;
const int inf = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const ll mod = 1e9 + 7 ;
const double eps = 1e-7 ;
template <class T> void read(T &a)
{
	int f = 1 ; a = 0;
	char ch = getchar() ;
	while (!isdigit(ch)) { if (ch == '-') f = -1 ; ch = getchar() ; }
	while (isdigit(ch)) { a =  (a << 3) + (a << 1) + ch - '0' ; ch = getchar() ; }
	a *= f;
}

ll n, ans;

ll p[maxn];

ll st[maxn][25], lg[maxn];

void init()
{
	rep(i, 1, n) st[i][0] = p[i];
	rep(i, 1, 20) rep(j, 1, n) st[j][i] = max(st[j][i - 1], st[j + (1 << (i - 1))][i - 1]);
	ll tot = 0;
	rep(i, 1, n)
	{
		if((1 << (tot + 1)) < i) tot ++;
		lg[i] = tot;
	}
	return ;
}

ll query(ll l, ll r)
{
	ll len = r - l + 1;
	// printf("%lld\n", r - (1 << lg[len]) + 1);
	return max(st[l][lg[len]], st[r - (1 << lg[len]) + 1][lg[len]]);
}

ll bsearch(ll l, ll r, ll x, ll typ)
{
	ll s = l, e = r;
	ll ret = typ * (n + 1);
	while(l <= r)
	{
		// printf("%lld %lld\n", l, r);
		ll mid = (l + r) >> 1;
		if(typ == 1)
		{
			if(query(mid, s) > x) r = mid - 1, ret = mid;
			else l = mid + 1;
		}
		else
		{
			if(query(mid, e) > x) l = mid + 1, ret = mid;
			else r = mid - 1;
		}
	}
	return ret;
}

signed main()
{
	scanf("%lld",  &n);
	rep(i, 1, n) scanf("%lld", &p[i]);
	init();
	// printf("%lld\n", query(4, 6));
	rep(i, 1, n)
	{
		ll tl = bsearch(1, i - 1, p[i], 0), tr = bsearch(i + 1, n, p[i], 1);
		ll sl = bsearch(1, tl - 1, p[i], 0), sr = bsearch(tr + 1, n, p[i], 1);
		// printf("%lld %lld %lld %lld %lld\n", tl, sl, tr, sr, i);
		ans += p[i] * (sr - tr) * (i - tl);
		ans += p[i] * (tl - sl) * (tr - i);
		// if(tl != 0 && tr != n + 1) ans -= p[i], puts("Yes"), tmp -= p[i];
	}
	printf("%lld\n", ans);
	return 0;
}