#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
template <typename Monoid>
struct RMQ {
  RMQ(int sz, const Monoid &UNITY) : UNITY(UNITY) {
    init(sz);
    dat.assign((n << 1) - 1, UNITY);
  }

  RMQ(const vector<Monoid> &a, const Monoid &UNITY) : UNITY(UNITY) {
    int a_sz = a.size();
    init(a_sz);
    dat.resize((n << 1) - 1);
    REP(i, a_sz) dat[n - 1 + i] = a[i];
    for (int i = n - 2; i >= 0; --i) dat[i] = max(dat[(i << 1) + 1], dat[(i << 1) + 2]);
  }

  void update(int node, Monoid value) {
    node += n - 1;
    dat[node] = value;
    while (node > 0) {
      node = (node - 1) >> 1;
      dat[node] = max(dat[(node << 1) + 1], dat[(node << 1) + 2]);
    }
  }

  Monoid query(int a, int b) { return query(a, b, 0, 0, n); }

  int find(int a, int b, Monoid value) { return find(a, b, value, 0, 0, n); }

  Monoid operator[](const int idx) const { return dat[idx + n - 1]; }

private:
  int n = 1;
  const Monoid UNITY;
  vector<Monoid> dat;

  void init(int sz) { while (n < sz) n <<= 1; }

  Monoid query(int a, int b, int node, int left, int right) {
    if (right <= a || b <= left) return UNITY;
    if (a <= left && right <= b) return dat[node];
    return max(query(a, b, (node << 1) + 1, left, (left + right) >> 1), query(a, b, (node << 1) + 2, (left + right) >> 1, right));
  }

  int find(int a, int b, Monoid value, int node, int left, int right) {
    if (dat[node] < value || right <= a || b <= left) return -1;
    if (right - left == 1) return node - (n - 1);
    int res_l = find(a, b, value, (node << 1) + 1, left, (left + right) >> 1);
    if (res_l != -1) return res_l;
    return find(a, b, value, (node << 1) + 2, (left + right) >> 1, right);
  }
};
// RMQ<要素の型> rmq(sz (a), 単位元) = 要素数 sz (数列 a) の RMQ を考える
// rmq.update(node, value) = a[node] を value に更新
// rmq.query(a, b) = min{a[i] : a <= i < b}
// rmq.find(a, b, value) = min{i : a[i] <= value (a <= i < b)}. 存在しない場合は -1
// rmq[idx] = a[idx]

int n;
int g(int idx) { return n - idx - 1; }

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  cin >> n;
  vector<int> idx(n + 1), p(n);
  REP(i, n) {
    cin >> p[i];
    idx[p[i]] = i;
  }
  RMQ<int> rmq(p, -INF);
  reverse(ALL(p));
  RMQ<int> rev(p, -INF);
  long long ans = 0;
  for (int i = n - 1; i >= 1; --i) {
    int l = rev.find(g(idx[i]) + 1, n, i + 1), r = rmq.find(idx[i] + 1, n, i + 1);
    // cout << (l == -1 ? l : g(l)) << ' ' << r << '\n';
    if (l != -1) {
      l = g(l);
      int l_hashi = rev.find(g(l) + 1, n, i + 1);
      int hidari = (l_hashi == -1 ? l + 1 : l - g(l_hashi));
      int migi = (r == -1 ? n - idx[i] : r - idx[i]);
      // cout << i << ':' << l - l_hashi << ' ' << migi << '\n';
      ans += (long long)i * hidari * migi;
    }
    if (r != -1) {
      int r_hashi = rmq.find(r + 1, n, i + 1);
      int hidari = (l == -1 ? idx[i] + 1 : idx[i] - l);
      int migi = (r_hashi == -1 ? n - r : r_hashi - r);
      // cout << hidari << ' ' << (r_hashi - r) << '\n';
      ans += (long long)i * hidari * migi;
    }
  }
  cout << ans << '\n';
  return 0;
}


Measured memory_bytes: 3072