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: Time Limit Exceeded)

class SegmentTree:
    """
    値をlistで持つSegmentTree
    update(i, x): i(0-indexed)をxで更新
    get(a,b): [a, b)を取得
    """

    def __init__(self, n, ini, merge):
        self.n = n
        n2 = 1  # nより大きい2の冪数
        while n2 < n:
            n2 <<= 1
        self.n2 = n2
        self.tree = [ini[:] for _ in range(n2 << 1)]
        self.ini = ini
        self.merge = merge

    def update(self, i, x):
        i += self.n2
        self.merge(self.tree[i], x)
        while i > 1:
            self.merge(x, self.tree[i ^ 1])
            i >>= 1
            self.tree[i][:] = x

    def get(self, a, b):
        result = self.ini[:]
        self._get(a, b, 1, 0, self.n2, result)
        return result

    def _get(self, a, b, k, l, r, result):
        """
        [a, b)の最小値を得る内部関数
        :param k: 現在調べている区間のtree内index
        :param l, r: kが表す区間の左右端index [l, r)
        :return: kが表す区間と[a, b)の共通区間内での値
        """
        # 範囲外ならINF
        if r <= a or b <= l:
            return
            # [a,b)が完全に[l,r)を包含するならtree[k]の値を採用
        if a <= l and r <= b:
            self.merge(result, self.tree[k])
            return
        # 一部だけ範囲内なら2つに分けて再帰的に調査
        m = (l + r) // 2
        self._get(a, b, k << 1, l, m, result)
        self._get(a, b, (k << 1) + 1, m, r, result)


def solve(n, ppp):
    def merge_min(x, y):
        """ update x """
        if x[0] <= y[0]:
            if x[1] <= y[0]:
                pass
            else:
                x[1] = y[0]
        elif x[0] <= y[1]:
            x[1] = x[0]
            x[0] = y[0]
        else:
            x[0] = y[0]
            x[1] = y[1]

    def merge_max(x, y):
        """ update x """
        if x[0] >= y[0]:
            if x[1] >= y[0]:
                pass
            else:
                x[1] = y[0]
        elif x[0] >= y[1]:
            x[1] = x[0]
            x[0] = y[0]
        else:
            x[0] = y[0]
            x[1] = y[1]

    str = SegmentTree(n, [n, n], merge_min)
    right = [None] * n
    for i in range(n - 1, -1, -1):
        p = ppp[i]
        right[i] = str.get(p, n)
        str.update(p - 1, [i, n])

    ans = 0
    stl = SegmentTree(n, [-1, -1], merge_max)
    for i in range(n):
        p = ppp[i]
        l1, l2 = stl.get(p, n)
        r1, r2 = right[i]
        ans += p * ((l1 - l2) * (r1 - i) + (r2 - r1) * (i - l1))
        stl.update(p - 1, [i, -1])

    return ans


n = int(input())
ppp = list(map(int, input().split()))
print(solve(n, ppp))