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
import java.util.*;
import java.util.stream.*;
import java.io.*;

public class Main {
    
  public static void main(String[] args) throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String[] in1 = br.readLine().split(" ");
    int N = Integer.parseInt(in1[0]);

    in1 = br.readLine().split(" ");
    int[] A = Stream.of(in1).mapToInt(Integer::parseInt).toArray();
    br.close();

    int[] B = new int[N];
    for(int i = 0; i < N; i++){
      B[A[i] - 1] = i;
    }

    TreeSet<Integer> ts = new TreeSet<>();
    long ans = 0L;
    for(int i = 0; i < N; i++){
      ts.add(B[i]);
      
      int right = ts.higher(B[i]) == null ? N : ts.higher(B[i]);
      int left = ts.lower(B[i]) == null ? -1 : ts.lower(B[i]);
      
      ans += (long)(i + 1) * (long)(B[i] - left) * (long)(right - B[i]);
    }

    System.out.println(ans);
  }
}