#include <bits/stdc++.h>

/////////////////////////
// Range Maximum Query //
/////////////////////////

class RMaxQ {
private:
	std::vector<int64_t> container_;
	const int64_t negative_inf_{LLONG_MIN};

	void build(const unsigned int array_size)
	{
		unsigned int length{1};
		while (length < array_size)
			length <<= 1;
		container_.resize(2 * length, negative_inf_);
	}

public:
	RMaxQ(const unsigned int array_size) { build(array_size); }
	RMaxQ(const std::vector<int64_t> &array)
	{
		build(array.size());
		std::copy(array.begin(), array.end(), container_.begin() + (container_.size() >> 1));
		for (auto i{(container_.size() >> 1) - 1}; i > 0; i--)
			container_[i] = std::max(container_[2 * i], container_[2 * i + 1]);
	}
	// indexは0-indexed
	void update(const int index, const int64_t assigned)
	{
		auto update_place{(container_.size() >> 1) + index};
		container_[update_place] = assigned;
		while (update_place > 1)
		{
			update_place >>= 1;
			container_[update_place] = std::max(container_[2 * update_place], container_[2 * update_place + 1]);
		}
	}
	// left,rightは0-indexed、[left, right)の半開区間
	int64_t get(const int left, const int right) const
	{
		int64_t max{negative_inf_};
		for (int left_i{std::max(0, left) + ((int)container_.size() >> 1)}, right_i{std::min((int)container_.size() >> 1, right) + ((int)container_.size() >> 1)};
			left_i < right_i; left_i >>= 1, right_i >>= 1
			)
		{
			if (left_i & 1)
			{
				max = std::max(max, container_[left_i]);
				left_i++;
			}
			if (right_i & 1)
			{
				right_i--;
				max = std::max(max, container_[right_i]);
			}
		}
		return max;
	}
};

/////////////////////////
// Range Minimum Query //
/////////////////////////

class RMinQ {
private:
	std::vector<int64_t> container_;
	const int64_t inf_{LLONG_MAX};

	void build(const unsigned int array_size)
	{
		unsigned int length{1};
		while (length < array_size)
			length <<= 1;
		container_.resize(2 * length, inf_);
	}

public:
	RMinQ(const unsigned int array_size) { build(array_size); }
	RMinQ(const std::vector<int64_t> &array)
	{
		build(array.size());
		std::copy(array.begin(), array.end(), container_.begin() + (container_.size() >> 1));
		for (auto i{(container_.size() >> 1) - 1}; i > 0; i--)
			container_[i] = std::min(container_[2 * i], container_[2 * i + 1]);
	}
	// indexは0-indexed
	void update(const int index, const int64_t assigned)
	{
		auto update_place{(container_.size() >> 1) + index};
		container_[update_place] = assigned;
		while (update_place > 1)
		{
			update_place >>= 1;
			container_[update_place] = std::min(container_[2 * update_place], container_[2 * update_place + 1]);
		}
	}
	// left,rightは0-indexed、[left, right)の半開区間
	int64_t get(const int left, const int right) const
	{
		int64_t min{inf_};
		for (int left_i{std::max(0, left) + ((int)container_.size() >> 1)}, right_i{std::min((int)container_.size() >> 1, right) + ((int)container_.size() >> 1)};
			left_i < right_i; left_i >>= 1, right_i >>= 1
			)
		{
			if (left_i & 1)
			{
				min = std::min(min, container_[left_i]);
				left_i++;
			}
			if (right_i & 1)
			{
				right_i--;
				min = std::min(min, container_[right_i]);
			}
		}
		return min;
	}
};

int main()
{
	int N;
	scanf("%d", &N);
	std::vector<int> P(N + 2);
	for (int i{1}; i <= N; i++) scanf("%d", &P[i]);
	P[0] = P[N + 1] = N + 1;

	std::vector<int64_t> init(N + 2);
	std::vector<int> l2(N + 2), l1(N + 2), r1(N + 2), r2(N + 2);
	{
		RMaxQ rmq(init);
		for (int i{1}; i <= N; i++)
		{
			l1[i] = rmq.get(P[i] + 1, N + 2);
			l2[i] = std::max(rmq.get(P[i] + 1, P[l1[i]]), rmq.get(P[l1[i]] + 1, N + 2));
			rmq.update(P[i], i);
		}
	}
	{
		std::fill(init.begin(), init.end(), N + 1);
		RMinQ rmq(init);
		for (int i{N}; i >= 1; i--)
		{
			r1[i] = rmq.get(P[i] + 1, N + 2);
			r2[i] = std::min({(int64_t)N + 1, rmq.get(P[i] + 1, P[r1[i]]), rmq.get(P[r1[i]] + 1, N + 2)});
			rmq.update(P[i], i);
		}
	}

	int64_t ans{};
	for (int i{1}; i <= N; i++)
		ans += ((int64_t)(i - l1[i]) * (r2[i] - r1[i]) + (int64_t)(l1[i] - l2[i]) * (r1[i] - i)) * P[i];
	
	printf("%lld\n", ans);

	return 0;
}

Measured memory_bytes: 4992