Language: C++

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;

const int N = 1e5 + 5;
ll n, ans;
int Arr[N];
int data[2][N][2];
// L & R - index - first or second?

void calc(int d) {
	vector <int> vec1, vec2, add;

	for (int i = 0; i < n; i++) {
		data[d][i][0] = data[d][i][1] = -1;

		while (vec2.size() && Arr[vec2.back()] < Arr[i])
			vec2.pop_back();
		if (vec2.size())
			data[d][i][1] = max(data[d][i][1], vec2.back());


		while (vec1.size() && Arr[vec1.back()] < Arr[i]) {
			add.push_back(vec1.back());
			vec1.pop_back();
		}

		while (add.size()) {
			while (vec2.size() && Arr[vec2.back()] < Arr[add.back()])
				vec2.pop_back();

			vec2.push_back(add.back());
			add.pop_back();
		}

		if (vec1.size()) {
			data[d][i][0] = vec1.back();
			if (1 < vec1.size())
				data[d][i][1] = max(data[d][i][1], vec1[vec1.size() - 2]);
		}
		vec1.push_back(i);
	}

	vec1.clear();
	vec2.clear();
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> Arr[i];

	calc(0);
	reverse(Arr, Arr + n);
	calc(1);
	reverse(Arr, Arr + n);
	for (int i = 0; i < n; i++)
		for (int id = 0; id < 2; id++)
			if (data[1][i][id] != -1)
				data[1][i][id] = n - 1 - data[1][i][id];
	for (int i = 0, j = n - 1; i < j; i++, j--)
		swap(data[1][i], data[1][j]);
	
	for (int i = 0; i < n; i++) {
		ll l, r;
		if (data[0][i][0] != -1) {
			l = data[0][i][1] + 1;
			r = data[1][i][0] - 1;
			if (data[1][i][0] == -1)
				r = n - 1;

			ans += (data[0][i][0] - l + 1) * (r - i + 1) * Arr[i];
		}
		if (data[1][i][0] != -1) {
			l = data[0][i][0] + 1;
			r = data[1][i][1] - 1;
			if (data[1][i][1] == -1)
				r = n - 1;

			ans += (i - l + 1) * (r - data[1][i][0] + 1) * Arr[i];
		}
	}

	cout << ans << "\n";
	return 0;
}

Compiler output:
a.cc: In function 'void calc(int)':
a.cc:18:17: error: reference to 'data' is ambiguous
   18 |                 data[d][i][0] = data[d][i][1] = -1;
      |                 ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:2:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:11:5: note:                 'int data [2][100005][2]'
   11 | int data[2][N][2];
      |     ^~~~
a.cc:18:33: error: reference to 'data' is ambiguous
   18 |                 data[d][i][0] = data[d][i][1] = -1;
      |                                 ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:11:5: note:                 'int data [2][100005][2]'
   11 | int data[2][N][2];
      |     ^~~~
a.cc:23:25: error: reference to 'data' is ambiguous
   23 |                         data[d][i][1] = max(data[d][i][1], vec2.back());
      |                         ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:11:5: note:                 'int data [2][100005][2]'
   11 | int data[2][N][2];
      |     ^~~~
a.cc:23:45: error: reference to 'data' is ambiguous
   23 |                         data[d][i][1] = max(data[d][i][1], vec2.back());
      |                                             ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:11:5: note:                 'int data [2][100005][2]'
   11 | int data[2][N][2];
      |     ^~~~
a.cc:40:25: error: reference to 'data' is ambiguous
   40 |                         data[d][i][0] = vec1.back();
      |                         ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:11:5: note:                 'int data [2][100005][2]'
   11 | int data[2][N][2];
      |     ^~~~
a.cc:42:33: error: reference to 'data' is ambiguous
   42 |                                 data[d][i][1] = max(data[d][i][1], vec1[vec1.size() - 2]);
      |                                 ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:11:5: note:                 'int data [2][100005][2]'
   11 | int data[2][N][2];
      |     ^~~~
a.cc:42:53: error: reference to 'data' is ambiguous
   42 |                                 data[d][i][1] = max(data[d][i][1], vec1[vec1.size() - 2]);
      |                                                     ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:11:5: note:                 'int data [2][100005][2]'
   11 | int data[2][N][2];
      |     ^~~~
a.cc: In function 'int main()':
a.cc:63:29: error: reference to 'data' is ambiguous
   63 |                         if (data[1][i][id] != -1)
      |                             ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:11:5: note:                 'int data [2][100005][2]'
   11 | int data[2][N][2];
      |     ^~~~
a.cc:64:33: error: reference to 'data' is ambiguous
   64 |                                 data[1][i][id] = n - 1 - data[1][i][id];
      |