#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
#define rep(i, n) for(ll i = 0; i < (n); i++)
#define repr(i, a, b) for(ll i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
const ll MOD = 1000000007;
const ll INF = 100000000000000000LL;
inline ll GCD(ll a, ll b){ return b?GCD(b, a % b):a; }
inline ll LCM(ll a, ll b){ return a/GCD(a, b)*b; }
inline ll powint(unsigned long long x, ll y){ ll r=1; while(y){ if(y&1) r*=x; x*=x; y>>=1; } return r; }
inline ll powmod(ll x, ll y, ll m = MOD){ ll r=1; while(y){ if(y&1) r*=x; x*=x; r%=m; x%=m; y>>=1; } return r; }
template<class T>bool chmax(T &a, const T &b){ if(a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b){ if(b<a) { a=b; return 1; } return 0; }
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif

struct SegmentTree{
    vll tree;
    int n;
    int n_ex;
    
    SegmentTree(const vll& vec){
        n = vec.size();
        int a = 1;
        while(a < n){
            a *= 2;
        }
        n_ex = a;
        // set tree
        // parent: floor((n-1)/2)
        // child: 2n+1, 2n+2
        // leaf: n+i-1
        rep(i, 2*n_ex-1) tree.emplace_back(-1);
        rep(i, n){
            update(i, vec[i]);
        }
    }
    
    // corresponds to vec[i] = x;
    void update(int i, ll x){
        i += n_ex - 1;
        tree[i] = x;
        while(i > 0){
            i = (i - 1) / 2;
            tree[i] = max(tree[i*2+1], tree[i*2+2]);
        }
    }
    
    // max( [a, b) );
    ll query(int a, int b, int k, int l, int r){
        if(r <= a || b <= l) return -1;
        if(a <= l && r <= b) return tree[k];
        else{
            ll vl = query(a, b, k*2+1, l, (l+r)/2);
            ll vr = query(a, b, k*2+2, (l+r)/2, r);
            return max(vl, vr);
        }
    }
    // from outside
    ll query(int a, int b){
        return query(a, b, 0, 0, n_ex);
    }
};

int main(){
    cin.tie(0); ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    ll n;
    cin >> n;
    vll p(n);
    vll idx(n+1, -1);
    rep(i, n){
        cin >> p[i];
        idx[p[i]] = i;
    }
    vll lidx1(n+1, -1);
    vll lidx2(n+1, -1);
    vll ridx1(n+1, n);
    vll ridx2(n+1, n);
    
    // 自身より大きい値のインデックス
    multiset<ll> ms{-1, -1, n, n};
    rep(i, n){
        ms.insert(i);
    }
    repr(i, 1, n+1){
        auto it = ms.find(idx[i]);
        lidx1[i] = *prev(prev(it));
        lidx2[i] = *prev(it);
        ridx1[i] = *next(it);
        ridx2[i] = *next(next(it));
        ms.erase(idx[i]);
    }
    
    ll ans = 0;
    dump(lidx1);
    dump(lidx2);
    dump(idx);
    dump(ridx1);
    dump(ridx2);
    repr(i, 1, n+1){
        ll tmp = (lidx2[i]-lidx1[i])*(ridx1[i]-idx[i])+(idx[i]-lidx2[i])*(ridx2[i]-ridx1[i]);
        tmp *= i;
        ans += tmp;
        dump(tmp, ans);
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 10880