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 PyPy3 (2.4.0) (judge verdict: Accepted)

import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

class BIT:
    '''
    a[1]~a[n]の数列を想定
    '''

    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def add(self, index, x):
        '''
        a[index]にxを加算
        '''
        while index <= self.size:
            self.tree[index] += x
            index += index & (-index)
 
    def sum(self, index):
        '''
        a[1]~a[index]の和
        '''
        s = 0
        while index:
            s += self.tree[index]
            index -= index & (-index)
        return s
 
    def search(self, value):
        '''
        sum(index)>=value を満たす最小のindex
        sum(n)<value のとき n+1 を返す
        '''
        i = 0
        s = 0
        step = 1 << (self.size.bit_length() - 1)
        while step:
            if i + step <= self.size and s + self.tree[i + step] < value:
                i += step
                s += self.tree[i]
            step //= 2
        return i + 1
        
        
def index_sort(A,reverse=False):
    x = []
    for i,a in enumerate(A):
        x.append((a,i))
    if reverse:
        return sorted(x, key=lambda x: -x[0])
    else:
        return sorted(x, key=lambda x: x[0])

      
N = I()
P = III()

bit = BIT(N)
x = index_sort(P,True)

ans = 0
for i in range(N):
    index = x[i][1]
    if index>=1:
        L = bit.sum(index)
    else:
        L = 0
    wi = bit.search(L-1) if L>=2 else 0
    xi = bit.search(L) if L>=1 else 0

    R = bit.sum(N)-bit.sum(index+1)
    yi = bit.search(L+1)
    zi = bit.search(L+2)

    ci = (xi-wi)*(yi-index-1) + (index+1-xi)*(zi-yi)
    ans += ci*x[i][0]
    bit.add(index+1, 1)

print(ans)