using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("3");
WillReturn.Add("2 3 1");
//5
}
else if (InputPattern == "Input2") {
WillReturn.Add("5");
WillReturn.Add("1 2 3 4 5");
//30
}
else if (InputPattern == "Input3") {
WillReturn.Add("8");
WillReturn.Add("8 2 7 3 4 5 6 1");
//136
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static long[] mPArr;
static long UB;
static void Main()
{
List<string> InputList = GetInputList();
mPArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();
UB = mPArr.GetUpperBound(0);
// 添字[値]なDict
var IndDict = new Dictionary<long, long>();
for (long I = 0; I <= mPArr.GetUpperBound(0); I++) {
IndDict[mPArr[I]] = I;
}
long[] SortedIndArr = IndDict.OrderByDescending(pX => pX.Key).Select(pX => pX.Value).ToArray();
var Ins_AVL_Set = new AVL_Set_MultiSet<long>();
var RangeInfoList = new List<RangeInfoDef>();
// 値の降順に見ていく
foreach (long EachInd in SortedIndArr) {
Ins_AVL_Set.Add(EachInd);
RangeInfoList.Add(GetPrevNextInfo(EachInd, Ins_AVL_Set));
}
long Answer = 0;
RangeInfoList = RangeInfoList.OrderBy(pX => pX.Ind).ToList();
foreach (RangeInfoDef EachRangeInfo in RangeInfoList) {
// 左の赤丸を取る場合
long PatternCntLeft = 0;
if (EachRangeInfo.Prev1Val > -1) {
long ProdLeft = 0;
if (EachRangeInfo.Prev2Val == -1) {
ProdLeft = EachRangeInfo.Prev1Val + 1;
}
else {
ProdLeft = EachRangeInfo.Prev1Val - EachRangeInfo.Prev2Val;
}
long ProdRight = 0;
if (EachRangeInfo.Next1Val == -1) {
ProdRight = UB - EachRangeInfo.Ind + 1;
}
else {
ProdRight = EachRangeInfo.Next1Val - EachRangeInfo.Ind;
}
PatternCntLeft = ProdLeft * ProdRight;
}
// 右の赤丸を取る場合
long PatternCntRight = 0;
if (EachRangeInfo.Next1Val > -1) {
long ProdLeft = 0;
if (EachRangeInfo.Prev1Val == -1) {
ProdLeft = EachRangeInfo.Ind + 1;
}
else {
ProdLeft = EachRangeInfo.Ind - EachRangeInfo.Prev1Val;
}
long ProdRight = 0;
if (EachRangeInfo.Next2Val == -1) {
ProdRight = UB - EachRangeInfo.Next1Val + 1;
}
else {
ProdRight = EachRangeInfo.Next2Val - EachRangeInfo.Next1Val;
}
PatternCntRight = ProdLeft * ProdRight;
}
Answer += EachRangeInfo.Val * (PatternCntLeft + PatternCntRight);
}
Console.WriteLine(Answer);
}
struct RangeInfoDef
{
internal long Ind;
internal long Val;
internal long Prev2Val;
internal long Prev1Val;
internal long Next1Val;
internal long Next2Val;
}
// 値を引数として、Setの前後2つまでのインデクサの値(なければ-1)を返す
static RangeInfoDef GetPrevNextInfo(long pCurrVal, AVL_Set_MultiSet<long> pIns_AVL_Set)
{
long CurrInd = pIns_AVL_Set.LowerBound(pCurrVal);
long Prev2Ind = CurrInd - 2;
long Prev1Ind = CurrInd - 1;
long Next1Ind = CurrInd + 1;
long Next2Ind = CurrInd + 2;
RangeInfoDef WillReturn;
WillReturn.Ind = pCurrVal;
WillReturn.Val = mPArr[pCurrVal];
WillReturn.Prev2Val = -1;
WillReturn.Prev1Val = -1;
WillReturn.Next1Val = -1;
WillReturn.Next2Val = -1;
if (0 <= Prev2Ind) WillReturn.Prev2Val = pIns_AVL_Set[(int)Prev2Ind];
if (0 <= Prev1Ind) WillReturn.Prev1Val = pIns_AVL_Set[(int)Prev1Ind];
if (Next1Ind <= pIns_AVL_Set.Count - 1) WillReturn.Next1Val = pIns_AVL_Set[(int)Next1Ind];
if (Next2Ind <= pIns_AVL_Set.Count - 1) WillReturn.Next2Val = pIns_AVL_Set[(int)Next2Ind];
return WillReturn;
}
// 値を引数として、Setの1小さいインデクサの値(なければ-1)を返す
static long GetPrevVal(long pCurrVal, AVL_Set_MultiSet<long> pIns_AVL_Set)
{
long CurrInd = pIns_AVL_Set.LowerBound(pCurrVal);
long PrevSetInd = CurrInd - 1;
if (0 <= PrevSetInd) {
return pIns_AVL_Set[(int)PrevSetInd];
}
else {
return -1;
}
}
// 値を引数として、Setの1大きいインデクサの値(なければ-1)を返す
static long GetNextVal(long pCurrVal, AVL_Set_MultiSet<long> pIns_AVL_Set)
{
long CurrInd = pIns_AVL_Set.LowerBound(pCurrVal);
long NextSetInd = CurrInd + 1;
if (NextSetInd <= pIns_AVL_Set.Count - 1) {
return pIns_AVL_Set[(int)NextSetInd];
}
else {
return -1;
}
}
}
#region AVL_Set_MultiSet
/// <summary>
/// 要素の追加、削除、検索、取得が可能な集合を表します.
/// </summary>
/// <typeparam name="T">優先度付きキュー内の要素の型を指定します.</typeparam>
/// <remarks>内部的にはAVL木によって実装されています.</remarks>
internal class AVL_Set_MultiSet<T>
{
Node root;
readonly IComparer<T> comparer;
readonly Node nil;
/// <summary>
/// 多重集合かどうかを表します.
/// </summary>
internal bool IsMultiSet { get; set; }
internal AVL_Set_MultiSet(IComparer<T> comparer)
{
nil = new Node(default(T));
root = nil;
this.comparer = comparer;
}
internal AVL_Set_MultiSet() : this(Comparer<T>.Default) { }
/// <summary>
/// 要素をコレクションに追加します.
/// </summary>
/// <remarks>この操作は計算量 O(log N) で実行されます.</remarks>
internal bool Add(T v)
{
return insert(ref root, v);
}
/// <summary>
/// v が存在するならコレクションから削除します.
/// </summary>
/// <remarks>この操作は計算量 O(log N) で実行されます.</remarks>
internal bool Remove(T v)
{
return remove(ref root, v);
}
/// <summary>
/// 0-indexed で index 番目の要素をコレクションから取得します.
/// </summary>
/// <remarks>この操作は計算量 O(log N) で実行されます.</remarks>
internal T this[int index] { get { return find(root, index); } }
internal int Count { get { return root.Count; } }
internal void RemoveAt(int k)
{
if (k < 0 || k >= root.Count) throw new ArgumentOutOfRangeException();
removeAt(ref root, k);
}
/// <summary>
/// このコレクションに含まれる要素を昇順に並べて返します.
/// </summary>
/// <remarks>この操作は計算量 O(N) で実行されます.</remarks>
internal T[] Items
{
get
{
var ret = new T[root.Count];
var k = 0;
walk(root, ret, ref k);
return ret;
}
}
private void walk(Node t, T[] a, ref int k)
{
if (t.Count == 0) return;
walk(t.lst, a, ref k);
a[k++] = t.Key;
walk(t.rst, a, ref k);
}
private bool insert(ref Node t, T key)
{
if (t.Count == 0) { t = new Node(key); t.lst = t.rst = nil; t.Update(); return true; }
int cmp = comparer.Compare(t.Key, key);
bool res;
if (cmp > 0)
res = insert(ref t.lst, key);
else if (cmp == 0) {
if (IsMultiSet) res = insert(ref t.lst, key);
else return false;
}
else res = insert(ref t.rst, key);
balance(ref t);
return res;
}
private bool remove(ref Node t, T key)
{
if (t.Count == 0) return false;
int cmp = comparer.Compare(key, t.Key);
bool ret;
if (cmp < 0) ret = remove(ref t.lst, key);
else if (cmp > 0) ret = remove(ref t.rst, key);
else {
ret = true;
var k = t.lst.Count;
if (k == 0) { t = t.rst; return true; }
if (t.rst.Count == 0) { t = t.lst; return true; }
t.Key = find(t.lst, k - 1);
removeAt(ref t.lst, k - 1);
}
balance(ref t);
return ret;
}
private void removeAt(ref Node t, int k)
{
var cnt = t.lst.Count;
if (cnt < k) removeAt(ref t.rst, k - cnt - 1);
else if (cnt > k) removeAt(ref t.lst, k);
else {
if (cnt == 0) { t = t.rst; return; }
if (t.rst.Count == 0) { t = t.lst; return; }
t.Key = find(t.lst, k - 1);
removeAt(ref t.lst, k - 1);
}
balance(ref t);
}
private void balance(ref Node t)
{
var balance = t.lst.Height - t.rst.Height;
if (balance == -2) {
if (t.rst.lst.Height - t.rst.rst.Height > 0) { rotR(ref t.rst); }
rotL(ref t);
}
else if (balance == 2) {
if (t.lst.lst.Height - t.lst.rst.Height < 0) rotL(ref t.lst);
rotR(ref t);
}
else t.Update();
}
private T find(Node t, int k)
{
if (k < 0 || k > root.Count) throw new ArgumentOutOfRangeException();
while (true) {
if (k == t.lst.Count) return t.Key;
else if (k < t.lst.Count) t = t.lst;
else { k -= t.lst.Count + 1; t = t.rst; }
}
}
/// <summary>
/// コレクションに含まれる要素であって、 v 以上の最小の要素の番号を返します。
/// </summary>
/// <remarks>この操作は計算量 O(log N) で実行されます.</remarks>
internal int LowerBound(T v)
{
var k = 0;
var t = root;
while (true) {
if (t.Count == 0) return k;
if (comparer.Compare(v, t.Key) <= 0) t = t.lst;
else { k += t.lst.Count + 1; t = t.rst; }
}
}
/// <summary>
/// コレクションに含まれる要素であって、 v より真に大きい、最小の要素の番号を返します。
/// </summary>
/// <remarks>この操作は計算量 O(log N) で実行されます.</remarks>
internal int UpperBound(T v)
{
var k = 0;
var t = root;
while (true) {
if (t.Count == 0) return k;
if (comparer.Compare(t.Key, v) <= 0) { k += t.lst.Count + 1; t = t.rst; }
else t = t.lst;
}
}
private void rotR(ref Node t)
{
var l = t.lst;
t.lst = l.rst;
l.rst = t;
t.Update();
l.Update();
t = l;
}
private void rotL(ref Node t)
{
var r = t.rst;
t.rst = r.lst;
r.lst = t;
t.Update();
r.Update();
t = r;
}
// 追加機能 LowerBoundで返したIndが、有効範囲かを判定
internal bool IsValidInd(int pInd)
{
if (pInd < 0) return false;
if (this.Count <= pInd) return false;
return true;
}
class Node
{
internal Node(T key)
{
Key = key;
}
internal int Count { get; private set; }
internal int Height { get; private set; }
internal T Key { get; set; }
internal Node lst, rst;
internal void Update()
{
Count = 1 + lst.Count + rst.Count;
Height = 1 + Math.Max(lst.Height, rst.Height);
}
public override string ToString()
{
return string.Format("Count = {0}, Key = {1}", Count, Key);
}
}
}
#endregion