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<bits/stdc++.h>
using namespace std;
int main(){
 int64_t N;
  cin>>N;
  map<int64_t,int64_t>M;
  for(int64_t i=1;i<=N;i++){
    int64_t a; cin>>a;
    M[a]=i;
  }set<int64_t>S;set<int64_t>S1;
  S.insert(0);S.insert(N+1);
  S1.insert(0);S1.insert(-N-1);
  int64_t ans=0;
  for(auto p:M){
  auto k=p.first;
    auto v=p.second;
    auto x=S.upper_bound(v);
    int64_t r=*x-v;
    S.insert(v);
    v*=-1;
    auto y=S1.upper_bound(v);
    int64_t l=*y-v;
    S1.insert(v);
    ans+=k*r*l;
  
  }cout<<ans<<endl;
 
  return 0;
}