#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;
const int INF = 1e9 + 5;
const double PI = acos(-1);

typedef long long ll;

const int X[] = {1, -1, 0, 0};
const int Y[] = {0, 0, 1, -1};

const int mod = 1e9 + 7;

int n, a[N], l[N], r[N];
ll ans;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    stack<int> s;
    for (int i = 1; i <= n; i++) {
        while (!s.empty() && a[s.top()] >= a[i]) {
            s.pop();
        }
        if (!s.empty()) {
            l[i] = s.top();
        } else {
            l[i] = 0;
        }
        s.push(i);
    }
    while (!s.empty()) {
        s.pop();
    }
    for (int i = n; i >= 1; i--) {
        while (!s.empty() && a[s.top()] >= a[i]) {
            s.pop();
        }
        if (!s.empty()) {
            r[i] = s.top();
        } else {
            r[i] = n + 1;
        }
        s.push(i);
    }
    for (int i = 1; i <= n; i++) {
        ans += (r[i] - i) * 1LL * (i - l[i]) * 1LL * a[i];
    }
    cout << ans;
    return 0;
}
