One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from his friend.

Find the following:

<image>

Constraints

* 1 ≦ N ≦ 200,000
* (a_1, a_2, ..., a_N) is a permutation of (1, 2, ..., N).

Input

The input is given from Standard Input in the following format:


N
a_1 a_2 ... a_N


Output

Print the answer.

Note that the answer may not fit into a 32-bit integer.

Examples

Input

3
2 1 3


Output

9


Input

4
1 3 2 4


Output

19


Input

8
5 4 8 1 2 6 7 3


Output

85
#include<iostream>
using namespace std;
typedef long long ll;
#define fr(i,n) for(int i=0;i<n;i++)

int main(){
  ll n,a[200010],b[200010],L[200010],R[200010],c=0,j,l,r,k;
  cin>>n;
  fr(i,n){
    cin>>a[i];
    b[a[i]]=i+1;
  }
  fr(i,n+2){
    L[i]=i;
    R[i]=i;
  }
  fr(i,n){
    j=n-i;
    k=b[j];
    l=L[k];
    r=R[k];
    c+=j*(k-l+1)*(r-k+1);
    L[r+1]=l;
    R[l-1]=r;
  }
  cout<<c<<endl;
}