#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T> using treap = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define REP(i, n) for (long long i = 0, max_i = (n); i < max_i; i++)
#define REPI(i, a, b) for (long long i = (a), max_i = (b); i < max_i; i++)
#define ALL(obj) begin(obj), end(obj)
#define RALL(obj) rbegin(obj), rend(obj)
#define fi first
#define se second
using ii = pair<int, int>;
vector<ii> dirs = {
    {1, 0}, {0, 1}, {-1, 0}, {0, -1},  // 4方向
    {1, 1}, {-1, 1}, {-1, -1}, {1, -1},  // 斜め
    {0, 0},  // 自身
};
template <class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; }
template <class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; }
template <class T, class S> vector<T> make_vec(size_t n, S x) { return vector<T>(n, x); }
template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...)); }

// debug
template <class T> ostream& operator<<(ostream& s, vector<T>& d) { REP (i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : " "); return s; }
template <class T> ostream& operator<<(ostream& s, vector<vector<T>>& d) { REP (i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : "\n"); return s; }
template <class T, class S> ostream& operator<<(ostream& s, pair<T, S>& p) { s << "{" << p.first << ", " << p.second << "}"; return s; }
template <class T, class S> ostream& operator<<(ostream& s, map<T, S> m) { for (auto it = m.begin(); it != m.end(); it++) { s << *it << (next(it) == m.end() ? "" : "\n"); } return s; }
template <class T, class S> ostream& operator<<(ostream& s, unordered_map<T, S> m) { for (auto it = m.begin(); it != m.end(); it++) { s << *it << (next(it) == m.end() ? "" : "\n"); } return s; }
#ifdef _MY_DEBUG
    #define dump(...) cerr << "/* " << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << endl, dump_func(__VA_ARGS__), cerr << "*/\n\n";
#else
    #define dump(...)
    #define endl "\n"
#endif
void dump_func() { cerr << endl; }
template <class Head, class... Tail> void dump_func(Head&& h, Tail&&... t) { cerr << h << (sizeof...(Tail) == 0 ? "" : ", "), dump_func(forward<Tail>(t)...); }

struct Fast { Fast() { cin.tie(nullptr); ios::sync_with_stdio(false); } } fast;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
constexpr int MOD = 1000000007;
// *************** TEMPLATE END *************** 

// 1次元上の要素のマージをしていくときに、各連結成分の[left, right)を更新
struct UnionFind {
    int cnt;                      // 集合の数
    vector<int> number;           // 0以上のとき親のindex, 負のときは集合サイズ
    vector<pair<int, int>> data;  // [left, right)
    UnionFind(int n) : cnt(n), number(n, -1), data(n) {
        for (int i = 0; i < n; i++) data[i] = {i, i + 1};
    }
    int root(int x) { return number[x] < 0 ? x : number[x] = root(number[x]); }
    void unite(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return;
        if (number[y] < number[x]) swap(x, y);
        // yをxにマージ
        number[x] += number[y];
        number[y] = x;
        // dataのマージ方法に合わせて変える
        data[x] = {min(data[x].first, data[y].first), max(data[x].second, data[y].second)};
        cnt--;
    }
    bool is_same(int x, int y) { return root(x) == root(y); }
    int size(int x) { return -number[root(x)]; }
    int& left(int x) { return data[root(x)].first; }
    int& right(int x) { return data[root(x)].second; }
    pair<int, int>& ref(int x) { return data[root(x)]; }
};

signed main() {
    int n; cin >> n;
    vector<int> b(n);
    REP (i, n) {
        int a; cin >> a;
        a--;
        b[a] = i + 1;
    }
    vector<bool> deleted(n + 2);
    UnionFind uf(n + 2);
    auto left = [&](int i) -> int {
        if (i == 0) return 0;
        if (!deleted[i - 1]) return i - 1;
        return uf.left(i - 1) - 1;
    };
    auto right = [&](int i) -> int {
        if (i == n + 1) return n + 1;
        if (!deleted[i + 1]) return i + 1;
        return uf.right(i + 1);
    };
    int ans = 0;
    REP (a, n) {
        int i = b[a];
        if (deleted[i - 1]) uf.unite(i - 1, i);
        if (deleted[i + 1]) uf.unite(i, i + 1);
        int l1 = left(i);
        int l2 = left(l1);
        int r1 = right(i);
        int r2 = right(r1);
        ans += (a + 1) * ((i - l1) * (r2 - r1) + (r1 - i) * (l1 - l2));
        deleted[i] = true;
    }
    cout << ans << endl;
}


Measured memory_bytes: 3456