#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void *wmem;
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16]={0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
inline void rd(int &x){
  int k, m=0;
  x=0;
  for(;;){
    k = getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void wt_L(char a){
  putchar_unlocked(a);
}
inline void wt_L(long long x){
  char f[20];
  int m=0, s=0;
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    putchar_unlocked('-');
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
template<class T> struct fenwick{
  T *data;
  int memory, size;
  void malloc(int mem){
    memory = mem;
    data = (T*)std::malloc(sizeof(T)*mem);
  }
  void walloc(int mem, void **workMemory=&wmem){
    memory = mem;
    walloc1d(&data, mem, workMemory);
  }
  void free(void){
    memory = 0;
    free(data);
  }
  void init(int N){
    size = N;
    memset(data,0,sizeof(T)*N);
  }
  void add(int k, T val){
    while(k < size){
      data[k] += val;
      k |= k+1;
    }
  }
  T get(int k){
    T res=0;
    while(k>=0){
      res += data[k];
      k = (k&(k+1))-1;
    }
    return res;
  }
  T range(int a, int b){
    if(b==-1){
      b=size-1;
    }
    return get(b) - get(a-1);
  }
  int kth(T k){
    T v;
    int c, i=0, j=size;
    while(i<j){
      c = (i+j)/2;
      v = get(c);
      if(v <= k){
        i=c+1;
      }
      else{
        j=c;
      }
    }
    return i==size?-1:i;
  }
}
;
char memarr[96000000];
int N;
int P[100000];
int inv[100000];
int main(){
  fenwick<int> f;
  int i, k, lef1, lef2, rig1, rig2;
  long long res;
  wmem = memarr;
  rd(N);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=0;Lj4PdHRW<(N);Lj4PdHRW++){
      rd(P[Lj4PdHRW]);P[Lj4PdHRW] += (-1);
    }
  }
  for(i=0;i<(N);i++){
    inv[P[i]] = i;
  }
  res = 0;
  f.malloc(N);
  f.init(N);
  for(k=N-1;k>=0;k--){
    int FmcKpFmN, KL2GvlyY, KrdatlYV, Q5VJL1cS, RZTsC2BF, WYIGIcGE, ao_dF3pO, cTE1_r3A, e98WHCEY, tU__gIr_, t_ynMSdg, xr20shxY;
    i = inv[k];
    KL2GvlyY = -1;
    Q5VJL1cS = i;
    while(KL2GvlyY < Q5VJL1cS){
      if((KL2GvlyY + Q5VJL1cS)%2==0){
        e98WHCEY = (KL2GvlyY + Q5VJL1cS) / 2;
      }
      else{
        e98WHCEY = (KL2GvlyY + Q5VJL1cS + 1) / 2;
      }
      if(f.range(e98WHCEY,i)>=1){
        KL2GvlyY = e98WHCEY;
      }
      else{
        Q5VJL1cS = e98WHCEY - 1;
      }
    }
    lef1 =Q5VJL1cS;
    cTE1_r3A = -1;
    RZTsC2BF = i;
    while(cTE1_r3A < RZTsC2BF){
      if((cTE1_r3A + RZTsC2BF)%2==0){
        FmcKpFmN = (cTE1_r3A + RZTsC2BF) / 2;
      }
      else{
        FmcKpFmN = (cTE1_r3A + RZTsC2BF + 1) / 2;
      }
      if(f.range(FmcKpFmN,i)>=2){
        cTE1_r3A = FmcKpFmN;
      }
      else{
        RZTsC2BF = FmcKpFmN - 1;
      }
    }
    lef2 =RZTsC2BF;
    xr20shxY = i;
    WYIGIcGE = N;
    while(xr20shxY < WYIGIcGE){
      if((xr20shxY + WYIGIcGE)%2==0){
        t_ynMSdg = (xr20shxY + WYIGIcGE) / 2;
      }
      else{
        t_ynMSdg = (xr20shxY + WYIGIcGE - 1) / 2;
      }
      if(f.range(i,t_ynMSdg)>=1){
        WYIGIcGE = t_ynMSdg;
      }
      else{
        xr20shxY = t_ynMSdg + 1;
      }
    }
    rig1 =WYIGIcGE;
    KrdatlYV = i;
    ao_dF3pO = N;
    while(KrdatlYV < ao_dF3pO){
      if((KrdatlYV + ao_dF3pO)%2==0){
        tU__gIr_ = (KrdatlYV + ao_dF3pO) / 2;
      }
      else{
        tU__gIr_ = (KrdatlYV + ao_dF3pO - 1) / 2;
      }
      if(f.range(i,tU__gIr_)>=2){
        ao_dF3pO = tU__gIr_;
      }
      else{
        KrdatlYV = tU__gIr_ + 1;
      }
    }
    rig2 =ao_dF3pO;
    res += (long long) (k+1) * (lef1-lef2) * (rig1 - i);
    res += (long long) (k+1) * (rig2-rig1) * (i - lef1);
    f.add(i,1);
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20190902-1

// --- original code ---
// int N, P[1d5];
// 
// int inv[1d5];
// {
//   int i, k;
//   int lef1, lef2, rig1, rig2;
//   ll res;
//   fenwick<int> f;
//   
//   rd(N,(P--)(N));
//   rep(i,N) inv[P[i]] = i;
// 
//   res = 0;
// 
//   f.malloc(N);
//   f.init(N);
//   for(k=N-1;k>=0;k--){
//     i = inv[k];
//     lef1 = bsearch_max[int,x,-1,i](f.range(x,i)>=1);
//     lef2 = bsearch_max[int,x,-1,i](f.range(x,i)>=2);
//     rig1 = bsearch_min[int,x,i,N](f.range(i,x)>=1);
//     rig2 = bsearch_min[int,x,i,N](f.range(i,x)>=2);
// 
//     res += (ll) (k+1) * (lef1-lef2) * (rig1 - i);
//     res += (ll) (k+1) * (rig2-rig1) * (i - lef1);
//     
//     f.add(i,1);
//   }
// 
//   wt(res);
// }


Measured memory_bytes: 1408