#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;

#ifdef LOCAL
	#define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr);
#else
	#define eprintf(...) 42
#endif

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template<typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
	return (ull)rng() % B;
}

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second

clock_t startTime;
double getCurrentTime() {
	return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}

const int N = 200200;
int n;
int a[N];

int st[N];
int sz;
int res[N][2];
ll solveStack() {
	sz = 0;
	st[sz++] = -1;
	for (int i = 0; i < n; i++) {
		while(sz > 1 && a[st[sz - 1]] > a[i]) sz--;
		res[i][0] = st[sz - 1];
		st[sz++] = i;
	}
	sz = 0;
	st[sz++] = n;
	for (int i = n - 1; i >= 0; i--) {
		while(sz > 1 && a[st[sz - 1]] > a[i]) sz--;
		res[i][1] = st[sz - 1];
		st[sz++] = i;
	}
	ll ans = 0;
	for (int i = 0; i < n; i++)
		ans += (ll)(i - res[i][0]) * (res[i][1] - i) * a[i];
	return ans;
}

int L[N], R[N];
pii ord[N];
ll solveSegm() {
	for (int i = 0; i < n; i++)
		ord[i] = mp(a[i], i);
	sort(ord, ord + n);
	for (int i = 0; i < n; i++)
		L[i] = R[i] = -1;
	ll ans = 0;
	for (int i = n - 1; i >= 0; i--) {
		int x = ord[i].second;
		int l = x, r = x;
		if (x > 0 && L[x - 1] != -1)
			l = L[x - 1];
		if (x + 1 < n && R[x + 1] != -1)
			r = R[x + 1];
		R[l] = r;
		L[r] = l;
		ans += (ll)(x - l + 1) * (r - x + 1) * ord[i].first;
	}
	return ans;
}

const int LOG = 18;
int sparse[LOG][N];
int p2[N];
int getMin(int l, int r) {
	int k = p2[r - l];
	return min(sparse[k][l], sparse[k][r - (1 << k)]);
}
ll solveSparse() {
	for (int i = 2; i < N; i++)
		p2[i] = p2[i / 2] + 1;
	for (int i = 0; i < n; i++)
		sparse[0][i] = a[i];
	for (int k = 0; k < LOG - 1; k++)
		for (int i = 0; i + (1 << (k + 1)) <= n; i++)
			sparse[k + 1][i] = min(sparse[k][i], sparse[k][i + (1 << k)]);
	ll ans = 0;
	for (int i = 0; i < n; i++) {
		int l = -1, r = i;
		while(r - l > 1) {
			int x = (l + r) / 2;
			if (getMin(x, i) <= a[i])
				l = x;
			else
				r = x;
		}
		ll L = i - l;
		l = i; r = n;
		while(r - l > 1) {
			int x = (l + r) / 2;
			if (getMin(i + 1, x + 1) < a[i])
				r = x;
			else
				l = x;
		}
		ll R = r - i;
		ans += L * R * a[i];
	}
	return ans;
}

int main()
{
	startTime = clock();
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	scanf("%d", &n);
	for (int i = 0; i < n; i++)
		scanf("%d", &a[i]);

	ll ans1 = solveStack();
	ll ans2 = solveSegm();
	ll ans3 = solveSparse();
	eprintf("%lld %lld %lld\n", ans1, ans2, ans3);
	assert(ans1 == ans2 && ans1 == ans3);
	printf("%lld\n", ans1);

	return 0;
}


Measured memory_bytes: 23100