/*
;<+111===1?=1=zzzzvI==llllltttttttrrrrrrrvvwXzzzzzzzzzzzzzzzzzzzzzzzzzzzuUXwzzXXzvvvvvvvvXXwrrrrrrrrttz-_...............
:;;;;;;;;;;;<<?<+??=====lllzOtOZOtttttrrrrw0vvzzzzzzzuuuuuuuuuuuuuuuuuuuuuuXWXXXUXzzzzwwvvvwUwvvrrrrrrttt+_.............
;:;:;;;;;<<<-(<<<<?<z1?===zOOOlllttttttrrrdXvzzzzzuuuuuXWXuuuuuuuuuuuuuuuuuuuWkuUyWXvOzXzzzvvzXwvrrrrrrrttO-~......`..`.
;;;;;;;+<~(<<~(<(?<+I??==zwOOOllllltttrwOrwkZwXuzuuuuuuuuXWkZuZZuuZZuZZuuuuuuuuuuuXSI1wkzvvvvvvwXwvrrrrrtttO_~..........
;>;>>+z<(+><<+<<?<(zI?==z0OZlllllltttrwIzrrXv+OWuuuuuuuZZZZZyWWkZyZZZZyZZZZZZZZuuuuXk?wkzzzvvvvrvZXrrrrZzttto~~~..~.....
>>>>jI+wI+=1z<+==1zdZllzZOZttltltttrwVOwvzzuWXzvWuZZZZZyyyWfWWyyWWWkVVyWWVyyyyZZZZZX0zXkuuzzvvvvrrrrrrtrzdO1z>~~~~.~.~..
>??zwZwOtOOtlttOOtOXWyrdkdrrrrrrvwwVOwuXWSuZWXWyvWXyyyyyyyVVVVWHWppWWkkWkWHWVyyyyyyXkXWWkXuuuzzvvvvrrrzzOzXy?O-~~~.~~.~~
??jZzzXzw0vrvwXvvwzXqHkWHkuuzuXXZXwXXWgHWZZyWyyWWAXWWVffffWffppHWppppppppppHkpffffWWWHyyHHXZZZuXXuuuuuzOOwwZkzI~~~~~~.~.
??1?=XXXSuXXZuuuuZZXHg@WHHyXkpWWkHH@HHHVyVVVWHffffWWWHWpppWbppbHMHbppppHWpbpbHWbpWHHpHWpW@NkWHWykXWXyZyZkwwXyXWc~~~~~~~~
???=zW0WWWyyyyyyyyVVW@@@@MHpWWH@M@HHppHppfpppHpppHpppWHmHbWHbbkHH@HkbbkbHHkkkkHHkkkkkHNkHHHNWHHpppkWHWpppppkXpffk+-~~~~~
1zuwXWyVVVVWWWWHpffppWMHH@HHHbbbWH@NkpHHppppWUHbbHkbbbkbbHHHkkmkHUMMHkkkqMMHqkkkHHqqqMMqM###MHMqkkkkHWHHHkqqkqqkkWpk&J_~
OOOOOOOWffKXppWMWbbpbbkHHHHNkkkkHHHHHHHXHHHHHmXHkHHkkkkkkkkHkkgkHKXXMNHHHHHWMNHgHHHHHM#H###N##MmggggmggHHH@HHgggHHHHqbpf
=====llWfpSdHbHHMHkkkkqMHHH#MHHHHMMZXHkuV4bbHXZ?HbHqkkbkkkkHHkHWkH_?4UWHqkHHXZWMHmMMMNNMNNNNNNMgg@H@@@@MNMMMMMM@MMgqR:::
==l=lllZWWktWkHMHHNHqqqMH#H#HMHqqkHuu<4kw(dWRw>~_4pHVHbbbbbbHWD?ZWn.__?OXWHqeJ?TXWHMN#MWMNNNNNM@@@M@@@@MNMM@MNNMM#HHq2::
llll=llldWRtOWkMH##HNHmM####HN1JWHHHHH#####HHMHms~?W21ZWfppppH$.._?l_jdHH#NNMNN#MMMM##NNgNMNNM@@@MM@@MMMMMMMMNNN#M2?4R::
lllllllllZWyttWHMM####MNM####N<WH#TC<dMMMM#NaJJJ<?!.?_.._?TWWXt.`.. ~._<jMMMMNNMNmm2<1w#MMNMM@@@MM@HMNMNNMNMNNN$7MP;;=::
llllllllllOUOttZHkHMH#NNN###HHr(HN-..(MBUMMMMHWK..````````` ?7wl.````..` WUZHMMMMWM$._dM3dM@@@MMHMMMMNMMNNMN#M#<;?<:::::
tltllllllllllllttXkrVM#NNNN##HN,_~7-..(S,..+zOd'`.``.```.`````` ````````. ?+._+zXV^.(?<(Hg@MMMMMNMMMNMNNMM#6?d3;::::::::
ttltlllllllllllllltttdHNNNNN#HMBH&...-.. _~~~~.`````````````````````.``.`````___.._.-JVY=dHNNMNNMNNMNNMB6?>;;;:::::::::~
tttttllllllllllllltttdM##NNN#HMc....```````````.``.``.````.``````````````.`````.`.._....~dH#NNMNMMMM#E??>;;;;:::::::::~:
rtttttttltllllllltlttwgHMNNNN#HMx.....`.````.````````````.._```````````.``.`.``.``.`....(M#NNMMM@@M@Mz?>>;;;;:::::::::::
rrtttttttltllltttllttrmHMMNNNN#HMe_..`.`.`.``.`````.``.``````````````.`````.`.`..`....-+M#NNMM@@@@M@R?>>;;;;;;;:;:::::::
rrrrttttttttttlttttttrWHM@MNNNN#MNO--..``.`````.``.`````````.`.`.`.```.`.``````....._(WBW#M@@@@@@@M@R??>>;;;;;:;::::::::
rrrrrrttttttlttltttttrdM@@MNMNNN#MOlz1--.....`.``````.``.`.``````.`.`.``.`.......-(g9C==d#M@@@@@@@M@R?>>>>;;;;;;;;;:::::
rrrrrrrrttttttttttttrrdM@@MNNMNN#MZll==z1(-...`..`.``.`.` .... .```.`.`......-(JWH9====zMM@@@H@H@@H@0??>>>>;;;;;;;;;;::;
vvvrrrrrrrrttttttttttrwMg@MMMMMNNH0tlll===?z1+--.....`.`.`.``..`...`....-((gMHHM8=====ldHM@@@@@@@@MgR=??>>>>>>;;;;;;;;;;
vrvrrrrrrrrrrrttttrrrrrdgg@MNMMNN#Rrtllll=======WHa-.-....`..`.....-((+WHHHWyy#Il====lldM@@H@@H@@@HgR=??>>>>>>>>>;;;;;;;
vrvvrrrrrtttrtrrtrtttrrdgggMN@MNN#Krtllllllll=lldkuuXUUG&---.-((JzuuuZZZyyyyyXRlllllllld@@@@@H@@@@MgS==???>>>>>>>;>>>;>;
vrvrrrrrrrrrttrttrrrrrrdHggM#@MNN##rtttllllllllltWuuZuuuuuuuuZuZZZZZZyyyyyyyyXSllllllltWg@@@HW@@@MMg0==?????>>>>>>>>>>>>
rrrrrrrrtrrtrttrtttrtrrdHqggMHHNN#NrtttttlltlllltdkuZZZZZZZZZZZZZZyZyyyyyyyyyWRtllllltdgg@@HHW@@@MgHI==????>??>>>>>>>>>>
rrrrrrrrttttttttttttttrwHkqqHMXM##MwrttttttttttttwKuuZZZZZZZZZZZZZZZZZZZyyyyyyHAwtttttdgggmHXHqmMHqKl=?????>??>??>??>???
vvvrrrtrtttttrttttttttrwHkkkkkkWHHHkrrtttOwwwwXUuXWuuuZuZZZZZZZZZZZZZZZZZZZZyyyyyyWWyqHHHkHZdXWHMkHS===??????????>??????
rrrrrtttttrttttttttttttrWpppppRZMHHkwXUUXvvvzzzzuuuuuuuuuuuuZuuZZZZZZZZZZZZZZZZZZZZZXHHHkH0wXSXHppW7?<11zz???>>?>??>?>??
rrrrrttttttllllzz??<<?v!jyVVXWfZdHMr1wwrvvvvvzzzzzzuuuuuuuuuuuuuuuuZZZZZZZZZZZZZZZZZWHSHkSdSXdWffV%~~~~~~<-__<??<<??>???
rrrtrttttv<!````````.?...UZyXZVWwM@b` ?1OZXUUUUUUUZZXkXuuZuuuuuuZuZXXXkkXXyyWWWWWUWH9=(HHXkHXHbpff~~~.~~~~(-````.``._?<z
rrrtttz^_`````.``.``.!...jZykXdfWX@H-...._?zrrvvzzuuuuuUUuuuuuuZXUuuuuuuuuuuuuuzVOV:.(WHWqqWqkkpW>~~~~~~~~~(-.``.```.`..
*/

#include <bits/stdc++.h>
#include <functional>
#include <iomanip>
using namespace std;
	
#define int long long
#define pb push_back
#define F first
#define S second
#define FOR(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define rep(i,n) FOR(i,0,n)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define ve vector
#define vi vector<int>
#define vp vector<pair<int,int>>
#define vvi vector<vector<int>>

template<typename T> using pq = priority_queue<T,vector<T>,greater<T>>; 
using ll = long long;
ll INF = LLONG_MAX / 4000 - 100;
ll mod = 1e9 + 7;
int dx[] = {-1,0,1,0}, dy[] = {0,1,0,-1};
vector<ll> prime;
	
class fact {
public:
	int fmod = 1e9+7;
	vector<int> fac, inv;
	fact (int n, int Mod = 1e9+7) {
		fmod = Mod;
		fac = vector<int>(n,0);
		inv = vector<int>(n,0);
		fac[0] = 1; for (int i = 1; i < n; i++) fac[i] = fac[i-1] * i % fmod;
		for (int i = 0;i < n;i++) inv[i] = fact::POW(fac[i],fmod-2);
	}
	ll nCr(ll n, ll r) {if(n < r) return 0; return (fac[n] * inv[r] % fmod) * inv[n-r] % fmod;}
	ll POW(ll a, ll b) {ll c = 1; while (b > 0) {if (b & 1) {c = a * c%fmod;}a = a * a%fmod; b >>= 1;}return c;}
    inline int operator [] (int i) {return fac[i];}
};
	
template <class T = ll> T in() {T x; cin >> x; return (x);}
void DEBUG(vector<int> a) {for(int i=0;i<a.size();i++)cout<<a[i]<<" ";cout<<endl;}
void EMP(int x) {cout<<"!!!"<<x<<"!!!"<<endl;}
ll GCD(ll a, ll b) {ll c; while (b != 0) {c = a % b; a = b; b = c;}return a;}
ll LCM(ll a, ll b) {return (a / GCD(a, b)) * (b / GCD(a, b)) * GCD(a, b);}
ll POW(ll a, ll b) {ll c = 1; while (b > 0) {if (b & 1) {c = a * c%mod;}a = a * a%mod; b >>= 1;}return c;}
void PRI(ll n) {bool a[n + 1LL]; for (int i = 0; i < n + 1LL; i++) {a[i] = 1LL;}for (int i = 2; i < n + 1LL; i++) {if (a[i]) {prime.pb(i); ll b = i; while (b <= n) {a[b] = 0; b += i;}}}}
template <typename T> T chmin(T& a, T b) {if(a>b)a=b;return a;}
template <typename T> T chmax(T& a, T b) {if(a<b)a=b;return b;}
bool isSqrt(ll a) {return pow(sqrt(a),2) == a ? 1 : 0;}
void YesNo(bool a) {if (a) cout << "Yes"; else cout << "No"; cout << endl;}
void yesno(bool a) {if (a) cout << "yes"; else cout << "no"; cout << endl;}
void YESNO(bool a) {if (a) cout << "YES"; else cout << "NO"; cout << endl;}
double dis(int x1, int x2, int y1, int y2) {
	return sqrt((double)abs(x1-x2)*(double)abs(x1-x2)+(double)abs(y1-y2)*(double)abs(y1-y2));
}

template<typename Monoid>
class SegTree {

//半開区間!!!!!!!!!!!!!!!
public:
    int p = 2;
    vector<Monoid> node;
    using Func = function<Monoid(Monoid,Monoid)>;
    Func pick; Monoid init;//操作と単位元

    SegTree(vector<Monoid> A, string type, Monoid m) {
        if (type == "min") {
            pick = [](int a, int b){return min(a,b);};
        } else if (type == "max") {
            pick = [](int a, int b){return max(a,b);};
        } else if (type == "sum") {
            pick = [](int a, int b){return a + b;};
        }

        init = m;
        while (A.size() > p) p *= 2;
        node = vector<Monoid>(2*p-1,init);

        for (int i = 0;i < A.size();i++) {
            node[p-1+i] = A[i];
        }

        for (int i = p - 2;i >= 0;i--) {
            node[i] = pick(node[i*2+1],node[i*2+2]);
        }
    }

    SegTree(vector<Monoid> A, Func f, Monoid m) {
        pick = f; init = m;
        while (A.size() > p) p *= 2;
        node = vector<Monoid>(2*p-1,init);

        for (int i = 0;i < A.size();i++) {
            node[p-1+i] = A[i];
        }

        for (int i = p - 2;i >= 0;i--) {
            node[i] = pick(node[i*2+1],node[i*2+2]);
        }
    }

    //1点更新
    void update(int i, Monoid v) {
        for (node[(i+=p)-1] = v; i >>= 1;) {
            node[i-1] = pick(node[i*2-1], node[i*2]);
        }
    }

    //1点加算
    void add(int i, Monoid v) {
        update(i, this[i] + v);
    }
 
    int getval(int start,int end,int k = 0,int l = 0,int r = -1) {
        if (r < 0) r = p;
        if (r <= start || end <= l) {return init;}
        if (start <= l && r <= end) {return node[k];}
        int L,R;
        L = getval(start,end,k*2+1,l,(l+r)/2);
        R = getval(start,end,k*2+2,(l+r)/2,r);
        return {pick(L,R)};
    }

    inline Monoid operator [] (int i) {
        return node[i+p-1];
    }

	int bsr(int pos, int num, int n) {
		int l, r; l = pos, r = n;
		if (getval(pos, r) < num) return n;
		if (getval(pos, r) == num) return l;
		while (r - l > 1) {
			int mid = (r + l) / 2;
			if (getval(pos, mid) > num) r = mid;
			else l = mid;
		}
		return l;
	}
	
	int bsl(int pos, int num, int n) {
		int l, r; l = 0, r = pos+1;
		if (getval(0, r) < num) return -1;
		if (getval(0, r) == num) return r-1;
		while (r - l > 1) {
			int mid = (r + l) / 2;
			if (getval(mid, pos+1) > num) l = mid;
			else r = mid;
		}
		return l;
	}
};

bool solve() {
	int n; cin >> n;
	vi p(n+1); rep (i, n) cin >> p[i]; p[n] = -1;
	SegTree<int> seg(p,"max",0);
	int ans = 0;
	rep (i, n) {
		int p1 = seg.bsr(i, p[i], n);
		int p2 = seg.bsr(p1+1, p[i], n);
		if (p2 == p1) p2 = n;
		int l1 = seg.bsl(i, p[i], n);
		int l2 = seg.bsl(l1-1, p[i], n);
		if (l1 == l2) l2 = -1;

		if (l1 == i && p1 == i) continue;
		if (l1 == i) {
			ans += p[i] * (i+1) * (p2 - p1);
		} else if (p1 == i) {
			ans += p[i] * (n-i) * (l1 - l2);
		} else {
			ans += p[i] * (i - l1) * (p2 - p1);
			ans += p[i] * (p1 - i) * (l1 - l2);
		}

	}
	cout << ans << endl;
}

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


Measured memory_bytes: 3840