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.copy() for _ in range(n2 << 1)]
        self.ini = ini
        self.merge = merge

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

    def get(self, a, b):
        return self._get(a, b, 1, 0, self.n2)

    def _get(self, a, b, k, l, r):
        """
        [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 self.ini
            # [a,b)が完全に[l,r)を包含するならtree[k]の値を採用
        if a <= l and r <= b:
            return self.tree[k]
        # 一部だけ範囲内なら2つに分けて再帰的に調査
        m = (l + r) // 2
        return self.merge(self._get(a, b, k << 1, l, m), self._get(a, b, (k << 1) + 1, m, r))


def solve(n, ppp):
    def merge_min(x, y):
        if x[0] <= y[0]:
            if x[1] <= y[0]:
                return x.copy()
            return [x[0], y[0]]
        if x[0] <= y[1]:
            return [y[0], x[0]]
        return y.copy()

    def merge_max(x, y):
        if x[0] >= y[0]:
            if x[1] >= y[0]:
                return x.copy()
            return [x[0], y[0]]
        if x[0] >= y[1]:
            return [y[0], x[0]]
        return y.copy()

    stl = SegmentTree(n, [-1, -1], merge_max)
    left1 = [-1] * n
    left2 = [-1] * n
    for i, p in enumerate(ppp):
        res = stl.get(p, n)
        left1[i] = res[0]
        left2[i] = res[1]
        stl.update(p - 1, [i, -1])

    str = SegmentTree(n, [n, n], merge_min)
    right1 = [n] * n
    right2 = [n] * n
    for i, p in enumerate(ppp[::-1]):
        i = n - i - 1
        res = str.get(p, n)
        right1[i] = res[0]
        right2[i] = res[1]
        str.update(p - 1, [i, n])
    # print(left1)
    # print(left2)
    # print(right1)
    # print(right2)

    ans = 0
    for i, l1, l2, r1, r2 in zip(range(n), left1, left2, right1, right2):
        ans += ppp[i] * ((l1 - l2) * (r1 - i) + (r2 - r1) * (i - l1))
    return ans


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


### FIXED CODE ###

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

    def update(self, i, x):
        # 注: 同じiへのクエリは複数来ないことを利用して単純化
        i += self.n2
        self.tree[i][0] = x
        while i > 1:
            i >>= 1
            sti = self.tree[i]
            if sti[0] > x:
                sti[1] = sti[0]
                sti[0] = x
            elif sti[1] > x:
                sti[1] = x

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

        while q:
            k, l, r = q.pop()

            if a <= l and r <= b:
                stk = self.tree[k]
                if result[0] > stk[0]:
                    result[1] = min(result[0], stk[1])
                    result[0] = stk[0]
                elif result[1] > stk[0]:
                    result[1] = stk[0]
                continue

            m = (l + r) // 2
            k <<= 1
            if a < m and l < b:
                q.append((k, l, m))
            if a < r and l < m:
                q.append((k + 1, m, r))

        return result


def solve(n, ppp):
    str = SegmentTree(n, n)
    right = []
    for i in range(n - 1, -1, -1):
        p = ppp[i]
        right.append(str.get(p, n))
        str.update(p - 1, i)
    right.reverse()

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

    return ans


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