#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <deque>
#include <iomanip>
#include <cstdio>
#include <stack>
#include <numeric>
#include <cassert>
using namespace std;
typedef  long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define  MP make_pair
#define  PB push_back
#define inf 1000000007
#define mod 1000000007
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define int long long

template<typename T> class segtree{
private:
    int n,sz;
    vector<T> node;
public:
    segtree(vector<T>& v) : sz((int)v.size()){
        n = 1;
        while(n < sz){
            n *= 2;
        }
        node.resize(2*n-1,0);
        for(int i = 0; i < sz; i++){
            node[i+n-1] = v[i];
        }
        for(int i=n-2; i>=0; i--){
            node[i] = node[i*2+1]+node[i*2+2];
        }
    }
    void update(int k,T a){
    	k += n-1;
    	node[k] = a;
    	while(k>0){
            k = (k-1)/2;
            node[k] = node[2*k+1]+node[2*k+2];
    	}
    }
    T query(int a,int b,int k=0,int l=0,int r=-1){
        if(r < 0) r = n;
    	if(r <= a || b <= l){
            return 0;
    	}
    	if(a <= l && r <= b){
            return node[k];
    	}else{
            T vl = query(a,b,2*k+1,l,(l+r)/2);
            T vr = query(a,b,2*k+2,(l+r)/2,r);
            return vl+vr;
    	}
    }
    void print(){
        for(int i = 0; i < sz; i++){
            cout<<query(i,i+1)<< " ";
        }
        cout<<endl;
    }
};

template<typename T> class segtreemx{
private:
    int n,sz;
    vector<T> node;
public:
    segtreemx(vector<T>& v) : sz((int)v.size()){
        n = 1;
        while(n < sz){
            n *= 2;
        }
        node.resize(2*n-1, numeric_limits<T>::min());
        for(int i = 0; i < sz; i++){
            node[i+n-1] = v[i];
        }
        for(int i=n-2; i>=0; i--){
            node[i] = max(node[i*2+1], node[i*2+2]);
        }
    }
    void update(int k,T a){
    	k += n-1;
    	node[k] = a;
    	while(k>0){
            k = (k-1)/2;
            node[k] = max(node[2*k+1],node[2*k+2]);
    	}
    }
    T query(int a,int b,int k=0,int l=0,int r=-1){
        if(r < 0) r = n;
    	if(r <= a || b <= l){
            return numeric_limits<T>::min();
    	}
    	if(a <= l && r <= b){
            return node[k];
    	}else{
            T vl = query(a,b,2*k+1,l,(l+r)/2);
            T vr = query(a,b,2*k+2,(l+r)/2,r);
            return max(vl,vr);
    	}
    }
    void print(){
        for(int i = 0; i < sz; i++){
            cout<<query(i,i+1)<< " ";
        }
        cout<<endl;
    }
};

signed main(){
    int n;
    cin >> n;
    vector<int>a(n);
    rep(i,n)cin >> a[i];
    vector<int>b(n),bb(n),c(n),cc(n);
    segtreemx<int>sgg(a);
    rep(i,n){
        int ng = -1;
        int ok = i;
        while(ok-ng>1){
            int mid = (ok+ng)/2;
            if(sgg.query(mid,i)>a[i]){
                ng = mid;
            }else{
                ok = mid;
            }
        }
        b[i] = ng;
        ng = -1;
        ok = b[i];
        while(ok-ng>1){
            int mid = (ok+ng)/2;
            if(sgg.query(mid,b[i])>a[i]){
                ng = mid;
            }else{
                ok = mid;
            }
        }
        bb[i] = ng;
        ng = n;
        ok = i;
        while(ng-ok>1){
            int mid = (ok+ng)/2;
            if(sgg.query(i,mid+1)>a[i]){
                ng = mid;
            }else{
                ok = mid;
            }
        }
        c[i] =ng;
        ng = n;
        ok = c[i];
        while(ng-ok>1){
            int mid = (ok+ng)/2;
            if(sgg.query(c[i]+1,mid+1)>a[i]){
                ng = mid;
            }else{
                ok = mid;
            }
        }
        cc[i] = ng;
    }
    ll sm = 0;
    rep(i,n){
        if(b[i]!=-1){
            sm += (b[i]-bb[i])*(c[i]-i)*a[i];
        }
        if(c[i]!=n){
            sm += (i-b[i])*(cc[i]-c[i])*a[i];
        }
        //cerr << i << " " << b[i] << " " << bb[i] << " " << c[i] << " " << cc[i] << " " << sm << endl;
    }
    cout << sm << endl;
    return 0;
}

Measured memory_bytes: 6272