#include <iostream>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;

#define endl '\n'

int n, a[1000000], l[1000000],r[1000000], pos[1000000], ans = 0;
stack <int> st,stt;
pair <int,int> p[1000000];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

    cin>>n;

    for (int i = 1;i<=n; i++) {
		cin>>a[i];
		pos[a[i]] = i;
    }
    for (int i = 1; i<=n; i++) {
        while (!st.empty() && a[st.top()]>=a[i]) {
            st.pop();
        }
        if (st.empty()) {
            l[i] = 0;
        }
        else {
            l[i] = st.top();
        }
        st.push(i);
    }
    for (int i = n; i>=1; i--) {
        while (!stt.empty() && a[stt.top()]>=a[i]) {
            stt.pop();
        }
        if (stt.empty()) {
            r[i] = n+1;
        }
        else {
            r[i] = stt.top();
        }
        stt.push(i);
    }
    for (int i = 1; i<=n; i++) {
		ans+=(r[i] - i)*(i - l[i])*a[i];
    }
    cout<<ans;


    return 0;
}

