# include <iostream>
# include <algorithm>
#include <array>
# include <cassert>
#include <cctype>
#include <climits>
#include <numeric>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <tuple>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
# include <chrono>
# include <random>
# include <limits.h>
# include <unordered_map>
# include <unordered_set>
# include <deque>
# include <cstdio>
# include <cstring>
#include <stdio.h>
#include<time.h>
#include <stdlib.h>
#include <cstdint>
#include <cfenv>
#include<fstream>
//#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7;
constexpr long long INF = numeric_limits<LL>::max() / 2;
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr<<#x<<": "<<x<<'\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll { LL first, second, third; };
struct Fll { LL first, second, third, fourd; };
typedef pair<LL, Tll> Ptll;
#define rep(i,rept) for(LL i=0;i<rept;i++)
#define Rrep(i,mf) for(LL i=mf-1;i>=0;i--)
void YN(bool f) {
	if (f)
		cout << "YES" << endl;
	else
		cout << "NO" << endl;
}
void yn(bool f) {
	if (f)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
}
struct Edge { LL to, cost; };
struct edge { LL from, to, cost; };
vector<vector<LL>>g;
vector<edge>edges;
vector<Pll>v;
map<Pll, Pll>ma;
set<LL>st;
LL h, w, n, m, k, t, s, p, q, last, cnt, sum, ans, a[210000];
Pll b[210000][2];
string str, ss;
bool f;
char c;
int di[4][2] = { { 0,1 },{ 1,0 },{ 0,-1 },{ -1,0 } };
struct Seg {
	LL n;
	vector<Pll>node, lazy;
	Seg(LL n_) {
		n = 1; while (n < n_)n *= 2;
		node.resize(n * 2 - 1);
		lazy.resize(n * 2 - 1);
		rep(i, n)node[n + i - 1] = Pll(-1, -1);
		Rrep(i, n - 2)node[i] = Pll(-1, -1);
		//区間最小などの時
		rep(i, n * 2 - 1)lazy[i] = Pll(-1,-1);
	}
	//区間の最小の遅延
	void ceval(LL k, LL l, LL r) {
		if (lazy[k] != Pll(-1,-1)) {
			if (lazy[k].second == -1)
				node[k] = Pll(lazy[k].first, node[k].first);
			else node[k] = lazy[k];
			if (r - l >= 1) {
				if (lazy[k].sec == -1) {
					lazy[k * 2 + 1] = Pll(lazy[k].fir, lazy[k * 2 + 1].fir);
					lazy[k * 2 + 2] = Pll(lazy[k].fir, lazy[k * 2 + 2].fir);
				}
				else {
					lazy[k * 2 + 1] = lazy[k];
					lazy[k * 2 + 2] = lazy[k];
				}
			}
			lazy[k] = Pll(-1,-1);
		}
	}
	void update(LL x, LL l, LL r) {
		update(0, x, 0, n - 1, l, r);
	}
	void update(LL i, LL x, LL a, LL b, LL l, LL r) {
		ceval(i, a, b);
		if (b < l || r < a)return;
		if (l <= a && b <= r) {
			lazy[i] = Pll(x,lazy[i].fir);
			ceval(i, a, b);
		}
		else {
			update(i * 2 + 1, x, a, (a + b) / 2, l, r);
			update(i * 2 + 2, x, (a + b + 1) / 2, b, l, r);
		}
	}
	Pll find(LL l, LL r) {
		return find(0, 0, n - 1, l, r);
	}
	Pll find(LL i, LL a, LL b, LL l, LL r) {
		ceval(i, a, b);
		if (b < l || r < a)return Pll(-1,-1);
		if (l <= a && b <= r)return node[i];
		Pll R = find(i * 2 + 2, (a + b + 1) / 2, b, l, r), L = find(i * 2 + 1, a, (a + b) / 2, l, r);
		if (R != Pll(-1, -1))return R;
		return L;
	}
};
map<int, int> zip;
int unzip[210000];

int compress(vector<LL> &x) {
	sort(x.begin(), x.end());
	x.erase(unique(x.begin(), x.end()), x.end());
	for (int i = 0; i < x.size(); i++) {
		zip[x[i]] = i;
		unzip[i] = x[i];
	}
	return x.size();
}
Seg seg1(210000), seg2(210000);
int main() {
	cin >> n;
	rep(i, n) {
		cin >> a[i];
	}
	vector<LL>V;
	rep(i, n) {
		V.push_back(a[i]);
	}
	compress(V);
	rep(i, n) {
		b[i][0] = seg1.find(zip[a[i]],zip[a[i]]);
		seg1.update(i, 0, zip[a[i]]);
		if (b[i][0].fir == -1)b[i][0].fir = -1;
		if (b[i][0].sec == -1)b[i][0].sec = -1;
	}
	Rrep(i, n) {
		b[i][1] = seg2.find(zip[a[i]],zip[a[i]]);
		seg2.update(i, 0, zip[a[i]]);
		if (b[i][1].fir == -1)b[i][1].fir = n;
		if (b[i][1].sec == -1)b[i][1].sec = n;
		ans += (((b[i][0].fir - b[i][0].sec)*(b[i][1].first - i)) +
			(((b[i][1].sec - b[i][1].fir)*(i - b[i][0].first))))*a[i];
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 44148