#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+2);
  f.init(N+2);
  f.add(0,2);
  f.add(N+1,2);
  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 = 0;
    Q5VJL1cS = i;
    while(KL2GvlyY < Q5VJL1cS){
      if((KL2GvlyY + Q5VJL1cS)%2==0){
        e98WHCEY = (KL2GvlyY + Q5VJL1cS) / 2;
      }
      else{
        e98WHCEY = (KL2GvlyY + Q5VJL1cS - 1) / 2;
      }
      if(f.range(e98WHCEY+1,i+1)==0){
        Q5VJL1cS = e98WHCEY;
      }
      else{
        KL2GvlyY = e98WHCEY + 1;
      }
    }
    lef1 =Q5VJL1cS- 1;
    cTE1_r3A = 0;
    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+1,i+1)<=1){
        RZTsC2BF = FmcKpFmN;
      }
      else{
        cTE1_r3A = FmcKpFmN + 1;
      }
    }
    lef2 =RZTsC2BF- 1;
    xr20shxY = i;
    WYIGIcGE = N-1;
    while(xr20shxY < WYIGIcGE){
      if((xr20shxY + WYIGIcGE)%2==0){
        t_ynMSdg = (xr20shxY + WYIGIcGE) / 2;
      }
      else{
        t_ynMSdg = (xr20shxY + WYIGIcGE + 1) / 2;
      }
      if(f.range(i+1,t_ynMSdg+1)==0){
        xr20shxY = t_ynMSdg;
      }
      else{
        WYIGIcGE = t_ynMSdg - 1;
      }
    }
    rig1 =WYIGIcGE+ 1;
    KrdatlYV = i;
    ao_dF3pO = N-1;
    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+1,tU__gIr_+1)<=1){
        KrdatlYV = tU__gIr_;
      }
      else{
        ao_dF3pO = tU__gIr_ - 1;
      }
    }
    rig2 =ao_dF3pO+ 1;
    res += (long long) (k+1) * (lef1-lef2) * (rig1 - i);
    res += (long long) (k+1) * (rig2-rig1) * (i - lef1);
    f.add(i+1,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+2);
//   f.init(N+2);
//   f.add(0,2);
//   f.add(N+1,2);
//   for(k=N-1;k>=0;k--){
//     i = inv[k];
//     lef1 = bsearch_min[int,x,0,i](f.range(x+1,i+1)==0) - 1;
//     lef2 = bsearch_min[int,x,0,i](f.range(x+1,i+1)<=1) - 1;
//     rig1 = bsearch_max[int,x,i,N-1](f.range(i+1,x+1)==0) + 1;
//     rig2 = bsearch_max[int,x,i,N-1](f.range(i+1,x+1)<=1) + 1;
//     
//     res += (ll) (k+1) * (lef1-lef2) * (rig1 - i);
//     res += (ll) (k+1) * (rig2-rig1) * (i - lef1);
//     
//     f.add(i+1,1);
//   }
// 
//   wt(res);
// }


Measured memory_bytes: 1408