#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define int long long
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e18;

template< typename T, typename E >
struct SegmentTree{
    using F = function< T(T, T) >;
    using G = function< T(T, E) >;
    int sz;
    vector<T> dat;
    F f;
    G g;
    T t1;
    SegmentTree(int n, F f, G g, T t1) : f(f), g(g), t1(t1) {
        sz = 1;
        while(sz < n) sz *= 2;
        dat.clear();
        dat.resize(2 * sz - 1, t1);
    }
    void set(int k, T x) { dat[k + sz - 1] = x; }
    void build() {
        for(int k = sz - 2; k >= 0; k--) {
            dat[k] = f(dat[2 * k + 1], dat[2 * k + 2]);
        }
    }
    void update(int k, E x) {
        k += sz - 1;
        dat[k] = g(dat[k], x);
        while(k > 0) {
            k = (k - 1) / 2;
            dat[k] = f(dat[2 * k + 1], dat[2 * k + 2]);
        }
    }
    T query(int a, int b, int k, int l, int r) {
        if(r <= a || b <= l) return t1;
        if(a <= l && r <= b) return dat[k];
        T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
        T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
        return f(vl, vr);
    }
    T query(int a, int b){ return query(a, b, 0, 0, sz); }
    T operator[](int k) { return dat[k + sz - 1]; }
};

signed main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int> p(n);
    rep(i, 0, n) cin >> p[i], p[i]--;
    SegmentTree<P, P> segl(n,
        [](P a, P b){ 
            vector<int> tmp;
            tmp.push_back(a.first);
            tmp.push_back(a.second);
            tmp.push_back(b.first);
            tmp.push_back(b.second);
            sort(tmp.rbegin(), tmp.rend());
            return P(tmp[0], tmp[1]); },
        [](P a, P b){ return b; },
        {-1, -1});
    SegmentTree<P, P> segr(n,
        [](P a, P b){ 
            vector<int> tmp;
            tmp.push_back(a.first);
            tmp.push_back(a.second);
            tmp.push_back(b.first);
            tmp.push_back(b.second);
            sort(tmp.begin(), tmp.end());
            return P(tmp[0], tmp[1]); },
        [](P a, P b){ return b; },
        {n, n});
    vector<int> left(n, -1), left2(n, -1), right(n, n), right2(n, n); // pos
    rep(i, 0, n){
        P tmp = segl.query(p[i] + 1, n);
        left[i] = tmp.first;
        left2[i] = tmp.second;
        segl.update(p[i], {i, -1});
    }
    for(int i = n - 1; i >= 0; i--){
        P tmp = segr.query(p[i] + 1, n);
        right[i] = tmp.first;
        right2[i] = tmp.second;
        segr.update(p[i], {i, n});
    }
    int ans = 0;
    rep(i, 0, n){
        // cout << i << ' ' << left[i] << ' ' << right[i] << endl;
        if(left[i] != -1){
            int tmpl = left[i] - left2[i];
            int tmpr = right[i] - i;
            // cout << " l " << tmpl << ' ' << tmpr << endl;
            ans += tmpl * tmpr * (p[i] + 1);
        }
        if(right[i] != n){
            int tmpl = i - left[i];
            int tmpr = right2[i] - right[i];
            // cout << " r " << tmpl << ' ' << tmpr << endl;
            ans += tmpl * tmpr * (p[i] + 1);
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 12416