Problem:
Score: 500 points

Problem Statement

Given is a permutation P of \{1, 2, \ldots, N\}.

For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest value among P_L, P_{L+1}, \ldots, P_R.

Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.

Constraints

2 \le N \le 10^5

1 \le P_i \le N

P_i \neq P_j (i \neq j)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 \ldots P_N

Output

Print \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.

Sample Input 1

3
2 3 1

Sample Output 1

5

X_{1, 2} = 2, X_{1, 3} = 2, and X_{2, 3} = 1, so the sum is 2 + 2 + 1 = 5.

Sample Input 2

5
1 2 3 4 5

Sample Output 2

30

Sample Input 3

8
8 2 7 3 4 5 6 1

Sample Output 3

136

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
	const int mod=(int)1e9+7;
	static void Main(){
		Sc sc=new Sc();
		var n=sc.I;
		var a=sc.Ia3(true,int.MaxValue,true,int.MaxValue);
		Spt2 spt=new Spt2(a);
		long ans2=0;
		for(int i = 1;i<=n;i++) {
			if(a[i]==n){continue;}
			int lb=i,ub=n+1,mid=0;
			while(ub-lb>1){
				mid=(ub+lb)/2;
				if(spt.Get(i,mid)>a[i]){ub=mid;}
				else{lb=mid;}
			}
			int r=ub;
			lb=0;ub=i;mid=0;
			while(ub-lb>1){
				mid=(ub+lb)/2;
				if(spt.Get(mid,i)>a[i]){lb=mid;}
				else{ub=mid;}
			}
			int l=lb;
			lb=r;ub=n+1;mid=0;
			while(ub-lb>1){
				mid=(ub+lb)/2;
				if(spt.Get(r+1,mid)>a[i]){ub=mid;}
				else{lb=mid;}
			}
			int r2=ub;
			lb=0;ub=l;mid=0;
			while(ub-lb>1){
				mid=(ub+lb)/2;
				if(spt.Get(mid,l-1)>a[i]){lb=mid;}
				else{ub=mid;}
			}
			int l2=lb;
			ans2+=a[i]*((long)l-l2)*(r-i);
			ans2+=a[i]*((long)i-l)*(r2-r);
		}
		Console.WriteLine("{0}",ans2);
	}
}
public class Spt{
	private readonly int[][] d;
	private readonly int[] l;
	public Spt(int[] a){
		int n=a.Length,m=(int)Log(n,2)+1;
		d=new int[m][];
		d[0]=new int[n];
		l=new int[n+1];
		for(int i=0;i<n;i++){
			d[0][i]=a[i];
			l[i+1]=l[i>>1]+1;
		}
		for(int i=1,k=1;i<m;i++,k<<=1){
			int p=n+1-(k<<1);
			d[i]=new int[p];
			for(int j=0;j<p;j++){d[i][j]=Max(d[i-1][j],d[i-1][j+k]);}
		}
	}
	public int Get(int a,int b){
		if(a>b){a^=b;b^=a;a^=b;}
		int p=l[b-a];
		return Max(d[p][a],d[p][b+1-(1<<p)]);
	}
}
public class Spt2{
	private int[][] he,l;
	private int[] ba;
	private int c;
	private Spt ma;
	public Spt2(int[] a){
		c=17;
		int n=(a.Length/c)+(a.Length%c==0?0:1);
		he=new int[n][];
		l=new int[n][];
		ba=new int[(1<<(c-1))+1];
		int[] ma=new int[n];
		for(int i = 1;i<c;i++) {ba[1<<i]=i;}
		for(int i = 0;i<n;i++) {
			int o=i!=n-1||(a.Length%c)==0?c:(a.Length%c);
			he[i]=new int[o];
			l[i]=new int[o];
			var ll=new LinkedList<int>();
			int p=0;
			for(int j = 0;j<o;j++) {
				he[i][j]=a[i*c+j];
				while(ll.Count>0&&he[i][ll.Last.Value]<=he[i][j]){
					p-=1<<ll.Last.Value;
					ll.RemoveLast();
				}
				ll.AddLast(j);
				p|=1<<j;
				l[i][j]=p;
			}
			ma[i]=a[i*c+ll.First.Value];
		}
		this.ma=new Spt(ma);
	}
	public int Get(int a,int b){
		if(a>b){a^=b;b^=a;a^=b;}
		int a1=a/c,b1=b/c;
		if(a1==b1){
			int q=l[a1][b%c]&~((1<<(a%c))-1);
			return he[a1][ba[q&-q]];
		}
		int f=l[a1][c-1]&~((1<<(a%c))-1);
		int g=l[b1][b%c];
		return a1+1<b1?Max(ma.Get(a1+1,b1-1),Max(he[a1][ba[f&-f]],he[b1][ba[g&-g]])):Max(he[a1][ba[f&-f]],he[b1][ba[g&-g]]);
	}
	static string Binary(long a,int b){
		string s=Convert.ToString(a,2);
		return (b>s.Length?new string('0',b-s.Length):"")+s;
	}
}

public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),int.Parse);}
	public int[] Ia3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),long.Parse);}
	public long[] La3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),long.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}