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 (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
int acs(const void *a, const void *b){return *(int*)a - *(int*)b;} /* 1,2,3,4.. */
int des(const void *a, const void *b){return *(int*)b - *(int*)a;} /* 8,7,6,5.. */
#define min(a,b) (a < b ? a: b)
#define max(a,b) (a > b ? a: b)

#define MAXN (100000)
#define MOD (998244353)


// Orderd Set 順序付き集合
// Treapで実装
typedef struct OSnode
{
    int val; //値
    struct OSnode* ch[2]; //左右の子
    int pri; //優先度
}OSnode;

OSnode* OSroot;

// rの子scを上に持ってくる
OSnode* OSrotate(OSnode*r, int sc)
{
    OSnode* nr = r->ch[sc];
    r->ch[sc] = nr->ch[1-sc];
    nr->ch[1-sc] = r;
    return nr;
}

OSnode* OSinsertSub(OSnode* r, OSnode* n)
{
    if(r==NULL) return n;
    int sc = r->val < n->val ? 1 : 0 ; //選択する子
    r->ch[sc] = OSinsertSub(r->ch[sc],n);

    if(r->pri > r->ch[sc]->pri) r = OSrotate(r,sc);
    return r;
}

void OSinsert(int val)
{
    OSnode* n = malloc(sizeof(OSnode));
    n->val = val;
    n->pri = rand();
    OSroot = OSinsertSub(OSroot, n);
}

// val 以上の最小の値のポインタを返す
OSnode* OSlower_bound(OSnode* r, int val)
{
    if(r==NULL) return NULL;
    else if(r->val >= val)
    {
        OSnode* res = OSlower_bound(r->ch[0], val);
        return res != NULL ? res : r;
    }else{
        return OSlower_bound(r->ch[1], val);
    }
}

// val 以下の最大の値のポインタを返す
OSnode* OSlower_boundRev(OSnode* r, int val)
{
    if(r==NULL) return NULL;
    else if(r->val <= val)
    {
        OSnode* res = OSlower_boundRev(r->ch[1], val);
        return res != NULL ? res : r;
    }else{
        return OSlower_boundRev(r->ch[0], val);
    }
}




int p[MAXN];
int ind[MAXN];

int main(void)
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&(p[i]));
        p[i]--;
        ind[p[i]] = i;
    }

    int64_t ans=0;
    for(int64_t x = n-1;x>=0;x--)
    {
        int i = ind[x];
        OSnode* lb = OSlower_boundRev(OSroot, i);
        int l1,l2;
        if(lb == NULL)
        {
            l1 = -1;
            l2 = -1;
        }else
        {
            l1 = lb->val;
            lb = OSlower_boundRev(OSroot, l1 -1);
            l2 = lb == NULL ? -1 : lb->val;
        }
        OSnode* rb = OSlower_bound(OSroot, i);
        int r1,r2;
        if(rb == NULL)
        {
            r1 = n;
            r2 = n;
        }else
        {
            r1 = rb->val;
            rb = OSlower_bound(OSroot, r1+1);
            r2 = rb == NULL ? n : rb->val;
        }
        ans += (((i-l1)*(r2-r1))+((r1-i)*(l1-l2))) * (x+1);
        OSinsert(i);
    }

    printf("%ld\n", ans);
    return 0;
}