Geek concert
Difficulty: Hard

The big country Geeko where every person is known as geek-ian, is organising a concert of geeks. The arrangements are all set for the concert. All **N** geek-ians are waiting in line to enter the concert. Geek-ians get bored waiting so they turn and look for someone familiar in the line. 

Two geek-ians P and Q standing in line can see each other if they're standing right next to each other or if **no geek-ian between them is strictly taller** than geek-ian P or geek-ian Q.
So till the entrance is not allowed into the concert, you should determine the number of pairs of geek-ians that can see each other.

**Input Format** 

The first line of input contains an integer N, the number of geek-ians standing in line. Next line will contains N integers, the height of each geek-ian in nanometres. Everyone will be shorter than 2^31 nanometres. The heights are given in the order in which geek-ians are standing in line.

**Output Format**  

Output the number of pairs of geek-ians that can see each other on a single line.

**Constraints** 

 1 &le; N &le; 500 000
 
**Sample Input**


     7
     2 4 1 2 2 5 1
     

**Sample Output**
 

	10
 

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):

	#include<iostream>
	#include<cmath>
	#include<cstring>
	#include<string>
	#include<bitset>
	#include<algorithm>
	#include<vector>
	#include<set>
	#include<map>
	#include<stack>
	#include<stdio.h>
	#include<queue>
	#define si(n) scanf("%d",&n)
	#define sll(n) scanf("%lld",&n)
	#define mod 1000000007 // 10**9 + 7
	#define INF 1e9
	#define FOR(i,a,b) for(int (i) = (a); (i) < (b); ++(i))
	#define RFOR(i,a,b) for(int (i) = (a)-1; (i) >= (b); --(i))
	#define CLEAR(a) memset((a),0,sizeof(a))
	#define mp(a, b) make_pair(a, b)
	#define pb(a) push_back(a)
	#define rep(i, a, b) for (int i = a; i < b; i++)
	#define rrep(i, b, a) for (int i = b; i > a; i--)
	#define all(v) v.begin(), v.end()
	#define GETCHAR getchar_unlocked
	#define pi(n) printf("%d\n",n)
	#define pll(n) printf("%lld\n",n)
	#define rk() int t; scanf("%d",&t); while(t--)
	using namespace std;
	const double pi = acos(-1.0);
	//freopen("in","r",stdin);
	//freopen("out","w",stdout);

	const int er[8] = {-1,-1,0,1,1,1,0,-1};
	const int ec[8] = {0,1,1,1,0,-1,-1,-1};
	const int fr[4] = {-1,1,0,0};
	const int fc[4] = {0,0,1,-1};
	typedef unsigned long long ull;
	typedef long long ll;
	typedef long l;
	typedef pair<int,int> pii;
	typedef vector<int> vec;
	typedef vector<pii> vpii;
	ll po(ll a,ll p)
	{ll ret = 1;while(p){if(p&1)ret = (ret*a)%mod;a=(a*a)%mod;p=p>>1;}return ret%mod;}

	stack<pii>S;
	int main(){
   		int n;
   		scanf("%d",&n);

   		ll ret = 0;
   		for(int i=0;i<n;++i){
      		int h;
      		scanf("%d",&h);
      		pii p(h,1);
      		for(;!S.empty() && S.top().first<=h;S.pop()){
         		ret += S.top().second;
         		if(S.top().first==h)p.second+=S.top().second;
      		}
      		if(!S.empty())++ret;
      		S.push(p);
   		}
   
   		cout<<ret<<endl;
   		return 0;
	}