Sherlock and Subarray
Difficulty: Hard

Watson gives an array $A_{1},A_{2}...A_{N}$ to Sherlock. He asks him to count the number of contiguous subarrays $A_{i},A_{i+1}...A_{j}$ such that $1 \leq i \leq j \leq N$ and the largest element of that subarray occurs only once in that subarray.    

**Input Format**  
First line contains $T$, the number of testcases.  
Each testcase contains an integer $N$ in the first line. This is followed by $N$ integers denoting the array $A$ in the second line.      

**Output Format**   
For each testcase, print the required answer in one line.    

**Constraints**    
$1 \leq T \leq 10$     
$1 \leq N \leq 10^{5}$     
$1 \leq A_{i} \leq 10^{5}$     

**Sample Input**  
	
    2
	3
    1 2 3
    4
    2 2 1 2
    
**Sample Output**  

	6
    6
    
**Explanation**  
Let's denote $A_{i},A_{i+1}...A_{j}$ by $S[i,j]$     

First testcase:   
All subarrays satisfy.    
Second testcase:    
$S[1,1], S[2,2], S[3,3], S[4,4], S[2,3], S[3,4]$ satisfy.

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):
    #include<bits/stdc++.h>
    using namespace std;
    #define pb push_back
    #define mp make_pair
    #define clr(x) x.clear()
    #define sz(x) ((int)(x).size())
    #define F first
    #define S second
    #define REP(i,a,b) for(i=a;i<b;i++)
    #define rep(i,b) for(i=0;i<b;i++)
    #define rep1(i,b) for(i=1;i<=b;i++)
    #define pdn(n) printf("%d\n",n)
    #define sl(n) scanf("%lld",&n)
    #define sd(n) scanf("%d",&n)
    #define pn printf("\n")
    typedef pair<int,int> PII;
    typedef vector<PII> VPII;
    typedef vector<int> VI;
    typedef vector<VI> VVI;
    typedef long long LL;
    #define INF 1000000007
    int main()
    {
        int n,arr[100009],i,j,k;
        int prntl[100009],prntr[100009];
        int t;
        cin >> t;
        while(t--)
        {
            cin >> n;
            for(i=0; i<n; i++)
                cin >> arr[i];
            prntl[0]=INF;
            prntr[n-1]=INF;
            for(i=1; i<n; i++)
            {
                if(arr[i]<=arr[i-1])prntl[i]=i-1;
                else
                {
                    j=prntl[i-1];
                    while(1)
                    {
                        if(j==INF || arr[j]>=arr[i])break;
                        j=prntl[j];
                    }
                    prntl[i]=j;
                }
            }
            for(i=n-2; i>=0; i--)
            {
                if(arr[i]<=arr[i+1])prntr[i]=i+1;
                else
                {
                    j=prntr[i+1];
                    while(1)
                    {
                        if(j==INF || arr[j]>=arr[i])break;
                        j=prntr[j];
                    }
                    prntr[i]=j;
                }
            }
            LL ans=0,p,q;
            for(i=0; i<n; i++)
            {
                if(prntl[i]==INF)p=i+1;
                else p=i-prntl[i];
                if(prntr[i]==INF)q=n-i;
                else q=prntr[i]-i;
                ans += (LL)p*(LL)q;
            }
            cout << ans << endl;
        }
        return 0;
    }