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>
#include <iostream>
#include <set>
using namespace std;

int main() {
	//受け取り、前処理
	long N;
	cin >> N;
	vector<pair<long, long>> V(N);
	for (long i = 0; i < N; i++){
		long a;
		cin >> a;
		V.at(i) = make_pair(a, i);
	}
	sort(V.begin(), V.end());

	//計算
	long ans = 0;
	std::set<long> S;
	S.insert(0);
	S.insert(N + 1);
	for (long i = 0; i < N; i++){
		long num = V.at(i).second + 1;
		decltype(S)::iterator r = S.lower_bound(num);
		auto l = r;
		l--;
		S.insert(num);
		ans += (num - *l) * (*r - num) * (i + 1);
	}
	cout << ans << endl;
}