#include <bits/stdc++.h>

using namespace std;
using ll = long long;
// #define int ll
using PII = pair<ll, ll>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()

template<typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template<typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }

template<typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts) {
    return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type
fill_v(T &t, const V &v) { t=v; }
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t, const V &v ) { for(auto &e:t) fill_v(e,v); }

template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a) {
    out<<'('<<a.first<<','<<a.second<<')'; return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
    out<<'[';
    for(const T &i: a) out<<i<<',';
    out<<']';
    return out;
}
template<class T>
ostream &operator <<(ostream& out, const set<T>& a) {
    out<<'{';
    for(const T &i: a) out<<i<<',';
    out<<'}';
    return out;
}
template<class T, class S>
ostream &operator <<(ostream& out, const map<T,S>& a) {
    out<<'{';
    for(auto &i: a) out<<i<<',';
    out<<'}';
    return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; // DRUL
const int INF = 1<<30;
const ll LLINF = 1LL<<60;
const ll MOD = 1000000007;

template<typename Monoid>
struct segtree {
    using T = typename Monoid::T;
    int n;
    vector<T> dat;

    segtree(int n_) {
        n = 1;
        while(n < n_) n <<= 1;
        dat.assign(n*2, Monoid::id());
    }
    void build(vector<T> v) {
        REP(i, v.size()) dat[i+n-1] = v[i];
        for(int i=n-1; i>0; --i) dat[i] = Monoid::op(dat[i*2+1], dat[i*2+2]);
    }

    T query(int a, int b) {
        T l = Monoid::id(), r = Monoid::id();
        for(a+=n, b+=n; a<b; a>>=1, b>>=1) {
            if(a&1) l = Monoid::op(l, dat[a++]);
            if(b&1) r = Monoid::op(dat[--b], r);
        }
        return Monoid::op(l, r);
    }
    void update(int i, T v) {
        i += n;
        dat[i] = v;
        while(i >>= 1) {
            dat[i] = Monoid::op(dat[i*2], dat[i*2+1]);
        }
    }

    friend ostream &operator <<(ostream& out,const segtree<Monoid>& seg){
        out << "---------------------" << endl;
        int cnt = 1;
        for(int i=1; i<=seg.n; i*=2) {
            REP(j, i) {
                out << seg.dat[cnt] << " ";
                cnt++;
            }
            out << endl;
        }
        out << "---------------------" << endl;
        return out;
    }
};

template<typename Tp>
struct max_monoid {
    using T = Tp;
    static constexpr Tp id() {
        return PII(-LLINF, -LLINF);
    }
    static Tp op(const Tp &a, const Tp &b) {
        ll ma = a.first, ma2 = a.second;
        if(b.first > ma) {
            ma2 = ma;
            ma = b.first;
        } else if(b.first > ma2) {
            ma2 = b.first;
        }
        if(b.second > ma) {
            ma2 = ma;
            ma = b.second;
        } else if(b.second > ma2) {
            ma2 = b.second;
        }
        return PII(ma, ma2);
    }
};

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll n;
    cin >> n;
    vector<ll> p(n);
    REP(i, n) cin >> p[i], p[i]--;

    segtree<max_monoid<PII>> seg(n);
    // REP(i, n) v[i] = PII(-LLINF, -LLINF);
    // seg.build(v);

    vector<PII> left(n);
    REP(i, n) {
        // cout << seg << endl;
        left[i] = seg.query(p[i], n);
        // if(left[i].first == -LLINF) left[i].first = i;
        if(left[i].second == -LLINF) left[i].second = -1;
        seg.update(p[i], PII(i, -LLINF));
    }

    segtree<max_monoid<PII>> seg2(n);
    vector<PII> right(n);
    for(ll i=n-1; i>=0; --i) {
        // cout << seg2 << endl;
        right[i] = seg2.query(p[i], n);
        right[i].first *= -1;
        right[i].second *= -1;
        // if(right[i].first == LLINF) right[i].first = i;
        if(right[i].second == LLINF) right[i].second = n;
        seg2.update(p[i], PII(-i, -LLINF));
    }

    // cout << left << endl;
    // cout << right << endl;

    ll ret = 0;
    REP(i, n) {
        // cout << "i=" << i << endl;
        if(left[i].first == -LLINF && right[i].first == LLINF) continue;
        if(left[i].first != -LLINF && right[i].first != LLINF) {
            ret += (p[i]+1) * (left[i].first-left[i].second) * (right[i].first-i);
            ret += (p[i]+1) * (i-left[i].first) * (right[i].second-right[i].first);
            // cout << "all:" << ret << endl;
        } else if(left[i].first != -LLINF) {
            ret += (p[i]+1) * (left[i].first-left[i].second) * (n-i);
            // cout << "left:" << ret << endl;
        } else if(right[i].first != LLINF) {
            ret += (p[i]+1) * (i+1) * (right[i].second-right[i].first);
            // cout << "right:" << ret << endl;
        }
    }
    cout << ret << endl;

    return 0;
}


Measured memory_bytes: 12416