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: Accepted)

/*#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")*/
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>
using namespace std;

#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define Str(x) to_string(x)
#define len(s) (int)s.size()
#define int long long
typedef long long ll;
typedef long double lld;
typedef string str;
typedef unsigned long long ull;

const int maxn = 1e5 + 228;

int first[maxn], second[maxn], arr[maxn], firstfirst[maxn], secondsecond[maxn];

int ind[maxn];


main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n;
    cin >> n;
    for (int i = 0; i < maxn; i++)
    {
        second[i] = n + 1;
        secondsecond[i] = n + 1;
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> arr[i];
        ind[arr[i]] = i;
    }
    int ans = 0;
    set <int> mx, mn;
    for (int i = n; i >= 1; i--)
    {
        if (!mx.empty() && *mx.rbegin() > ind[i])
        {
            int val = *mx.lower_bound(ind[i]);
            second[ind[i]] = val;
            if (*mx.rbegin() != val)
            {
                val = *mx.lower_bound(val + 1);
                secondsecond[ind[i]] = val;
            }
        }
        if (!mn.empty() && *mn.rbegin() > -ind[i])
        {
            int val = *mn.lower_bound(-ind[i]);
            first[ind[i]] = -val;
            if (*mn.rbegin() != val)
            {
                val = *mn.lower_bound(val + 1);
                firstfirst[ind[i]] = -val;
            }
        }
        mx.insert(ind[i]);
        mn.insert(-ind[i]);
    }
    for (int i = 1; i <= n; i++)
    {
        if (arr[i] == n)
        {
            continue;
        }

        int flexxx = 0;
        if (second[i] != n + 1)
        {
            flexxx += (secondsecond[i] - second[i]) * (i - first[i]);
        }
        if (first[i] != 0)
        {
            flexxx += (first[i] - firstfirst[i]) * (second[i] - i);
        }
        ans += flexxx * arr[i];
    }
    cout << ans;
    return 0;
}