Language: C++

int main() {
    cin >> N;

    long P[N];
    int value_index[N+1];
    for(int i = 0; i < N; i++) {
        cin >> P[i];
        value_index[P[i]] = i;
    }

    auto cmp = [](int a, int b) { return a > b;};

    set<int> larger_idx_right;
    set<int, decltype(cmp)> larger_idx_left(cmp);
    larger_idx_right.insert(value_index[N]);
    larger_idx_left.insert(value_index[N]);

    set<int>::iterator it;

    long ans = 0;

    for(int i = N-1; i > 0; i--) {
        int right_1 = N;
        int right_2 = N;
        it = larger_idx_right.upper_bound(value_index[i]);
        if(it != larger_idx_right.end()) {
            right_1 = *it;
            it = larger_idx_right.upper_bound(right_1);
            if(it != larger_idx_right.end()) {
                right_2 = *it;
            }
        }

        int left_1 = -1;
        int left_2 = -1;
        it = larger_idx_left.upper_bound(value_index[i]);
        if(it != larger_idx_left.end()) {
            left_1 = *it;
            it = larger_idx_left.upper_bound(left_1);
            if(it != larger_idx_left.end()) {
                left_2 = *it;
            }
        }

        larger_idx_right.insert(value_index[i]);
        larger_idx_left.insert(value_index[i]);

        if (right_1 != N) {
            ans += i*(value_index[i]-left_1)*(right_2-right_1);
        }
        if (left_1 != -1) {
            ans += i*(right_1-value_index[i])*(left_1-left_2);
        }
    }

    cout << ans << endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:2:5: error: 'cin' was not declared in this scope
    2 |     cin >> N;
      |     ^~~
a.cc:2:12: error: 'N' was not declared in this scope
    2 |     cin >> N;
      |            ^
a.cc:7:16: error: 'P' was not declared in this scope
    7 |         cin >> P[i];
      |                ^
a.cc:8:9: error: 'value_index' was not declared in this scope
    8 |         value_index[P[i]] = i;
      |         ^~~~~~~~~~~
a.cc:13:5: error: 'set' was not declared in this scope
   13 |     set<int> larger_idx_right;
      |     ^~~
a.cc:13:9: error: expected primary-expression before 'int'
   13 |     set<int> larger_idx_right;
      |         ^~~
a.cc:14:9: error: expected primary-expression before 'int'
   14 |     set<int, decltype(cmp)> larger_idx_left(cmp);
      |         ^~~
a.cc:15:5: error: 'larger_idx_right' was not declared in this scope
   15 |     larger_idx_right.insert(value_index[N]);
      |     ^~~~~~~~~~~~~~~~
a.cc:15:29: error: 'value_index' was not declared in this scope
   15 |     larger_idx_right.insert(value_index[N]);
      |                             ^~~~~~~~~~~
a.cc:16:5: error: 'larger_idx_left' was not declared in this scope
   16 |     larger_idx_left.insert(value_index[N]);
      |     ^~~~~~~~~~~~~~~
a.cc:18:9: error: expected primary-expression before 'int'
   18 |     set<int>::iterator it;
      |         ^~~
a.cc:25:9: error: 'it' was not declared in this scope; did you mean 'i'?
   25 |         it = larger_idx_right.upper_bound(value_index[i]);
      |         ^~
      |         i
a.cc:56:5: error: 'cout' was not declared in this scope
   56 |     cout << ans << endl;
      |     ^~~~
a.cc:56:20: error: 'endl' was not declared in this scope
   56 |     cout << ans << endl;
      |                    ^~~~