#include <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <time.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <numeric>
#include <limits>
#include <type_traits>


using namespace std;
#define SAY_YES cout << "YES" << endl;
#define SAY_Yes cout << "Yes" << endl;
#define SAY_NO cout << "NO" << endl;
#define SAY_No cout << "No" << endl;
#define IFYES(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "YES" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "NO" << endl;  \
    }
#define IFYes(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "Yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "No" << endl;  \
    }
#define IFyes(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "no" << endl;  \
    }
#define DEBUG_OUTPUT_ARRAY(XXX, ONE)                            \
    for (int i = 0; i < (ONE); i++)                             \
    {                                                           \
        cout << "DEBUG: i = " << i << " -> " << XXX[i] << endl; \
    }
#define DEBUG_OUTPUT_ARRAY2(XXX, ONE, TWO)                             \
    for (int i = 0; i < (ONE); i++)                                    \
    {                                                                  \
        cout << "<<< i = " << i << " >>>" << endl;                     \
        for (int j = 0; j < (TWO); j++)                                \
        {                                                              \
            cout << "DEBUG: j = " << j << " -> " << XXX[i][j] << endl; \
        }                                                              \
    }
#define DEBUG_OUTPUT_ARRAY2_BOX(XXX, ONE, TWO) \
    for (int i = 0; i < (ONE); i++)            \
    {                                          \
        cout << i << "  ";                     \
        for (int j = 0; j < (TWO); j++)        \
        {                                      \
            cout << XXX[i][j] << " ";          \
        }                                      \
        cout << endl;                          \
    }

typedef pair<long long int, long long int> pll;


const long long int mod = 1000000007;
const long long int INF = 1e18;
const long double PI=3.14159265358979323; 


long long int N,P[200200],locate[200200],res=0;

string str[1000];
set<long long int>s;
set<long long int,greater<long long int> >s2;

int main()
{
    cout << fixed << setprecision(18);
    cin>>N;
    for(long long int i = 1; i <= N; i++){
        cin>>P[i];
        locate[P[i]]=i;
    }
    s.insert(0);
    s.insert(N+1);
    
    
    s.insert(locate[N]);

    s2.insert(0);
    s2.insert(N+1);
    
    s2.insert(locate[N]);
    for(long long int i = N-1; i >=1; i--){
        auto itr = s.lower_bound(locate[i]);

        auto itr2= s.upper_bound(*itr);
        
        
        auto itr3 = s2.lower_bound(locate[i]);
    
        auto itr4= s2.upper_bound(*itr3);
        if((*itr)!=N+1){
            res+=((*itr2)-(*itr))*(locate[i]-(*itr3))*i;
        }
        if((*itr3)!=0){
            res+=((*itr)-locate[i])*((*itr3)-(*itr4))*i;
        }
        
        s.insert(locate[i]);
        s2.insert(locate[i]);
    }
    cout<<res<<endl;

    
    
}    


Measured memory_bytes: 11136