#include <chrono>

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <bitset>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <time.h>
#include <random>
#include <string>
#include <cassert>
#include <vector>
#include <ostream>
#include <istream>
#include <stack>
#include <deque>
#include <queue>
#include <functional>

using namespace std;

#define int long long
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define pii pair<int, int>
#define ld long double

ostream& operator << (ostream &a, const vector<int> &b) {
    for (auto k : b) cout << k << " ";
    return a;
}

#ifdef LOCAL
    #define dbg(x) cout << #x << " : " << (x) << "\n";
    const int INF = 1e18;
//    const int mod = 2600000069;
//    const int p = 10;
//    const ld PI = 3.1415926535;
#else
    #define dbg(x)
    const int INF = 1e18;
//    const int mod = 2600000069;
//    const int p = 179;
//    const ld PI = 3.1415926535;
#endif

//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,sse3,sse4")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("fast-math")
//#pragma GCC target("avx2")
//#pragma GCC optimize("section-anchors")
//#pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
//#pragma GCC optimize("vpt")
//#pragma GCC optimize("rename-registers")
//#pragma GCC optimize("move-loop-invariants")
//#pragma GCC optimize("unswitch-loops")
//#pragma GCC optimize("function-sections")
//#pragma GCC optimize("data-sections")

const int MAXN = 1e5 + 1;
const int LOG = 20;

int n;
int t[LOG][MAXN], lg[MAXN];
vector<int> a;

inline int query(int l, int r) {
    int pw = lg[r - l + 1];
    return max(t[pw][l], t[pw][r - (1 << pw) + 1]);
}

int pref(int i, int k) {
    if (i == -1) return -1;
    if (query(0, i) <= k) return -1;
    if (a[i] > k) return i;
    int l = 0, r = i, mid;
    while (r - l > 1) {
        mid = (l + r) / 2;
        if (query(mid, i) <= k) r = mid;
        else l = mid;
    }
    return l;
}

int suf(int i, int k) {
    if (i == n) return n;
    if (query(i, n - 1) <= k) return n;
    if (a[i] > k) return i;
    int l = i, r = n - 1, mid;
    while (r - l > 1) {
        mid = (l + r) / 2;
        if (query(i, mid) <= k) l = mid;
        else r = mid;
    }
    return r;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifdef LOCAL
    #else
//        freopen("capitals.in", "r", stdin);
//        freopen("capitals.out", "w", stdout);
    #endif
    cin >> n;
    a.resize(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    lg[1] = 0;
    lg[0] = 0;
    for (int i = 2; i < MAXN; i++) {
        lg[i] = lg[i >> 1] + 1;
    }
    for (int i = 0; i < n; i++) {
        t[0][i] = a[i];
    }
    for (int k = 1; (1 << k) < n; k++) {
        for (int i = 0; i < n - (1 << k) + 1; i++) {
            t[k][i] = max(t[k - 1][i], t[k - 1][i + (1 << (k - 1))]);
        }
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
        int l1 = pref(i - 1, a[i]), r1 = suf(i + 1, a[i]), l2 = -1, r2 = n;
        if (l1 != -1) l2 = pref(l1 - 1, a[i]);
        if (r1 != n) r2 = suf(r1 + 1, a[i]);
        ans += a[i] * (i - l1) * (r2 - r1);
        ans += a[i] * (l1 - l2) * (r1 - i);
    }
    cout << ans;
}
/*

*/


Measured memory_bytes: 15360