#include <bits/stdc++.h>

using namespace std;
using ll = long long;
// #define int ll
using PII = pair<ll, ll>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()

template<typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template<typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }

template<typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts) {
    return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type
fill_v(T &t, const V &v) { t=v; }
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t, const V &v ) { for(auto &e:t) fill_v(e,v); }

template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
    out<<'('<<a.first<<','<<a.second<<')'; return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
    out<<'['; for(T i: a) {out<<i<<',';} out<<']'; return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; // DRUL
const int INF = 1<<30;
const ll LLINF = 1LL<<60;
const ll MOD = 1000000007;

template <typename T>
struct segtree {
    using F = function<T(T,T)>;
    int n;
    vector<T> dat;
    F f, g;
    T d;

    segtree(int n_, F f_, F g_, T d_) : f(f_), g(g_), d(d_) {
        n = 1;
        while(n < n_) n *= 2;
        dat.assign(n*2, d);
    }
    void build(vector<T> v) {
        REP(i, v.size()) dat[i+n-1] = v[i];
        for(int i=n-2; i>=0; --i) dat[i] = f(dat[i*2+1], dat[i*2+2]);
    }

    T query(int a, int b, int k, int l, int r) {
        if(r <= a || b <= l) return d;
        if(a <= l && r <= b) return dat[k];
        return f(query(a, b, 2*k+1, l, (l+r)/2),
                query(a, b, 2*k+2, (l+r)/2, r));
    }
    T query(int a, int b) {return query(a, b, 0, 0, n);}
    void update(int i, T v) {
        i += n-1;
        dat[i] = g(dat[i], v);
            while(i > 0) {
            i = (i-1)/2;
            dat[i] = f(dat[i*2+1], dat[i*2+2]);
        }
    }

    void debug() {
        cout << "---------------------" << endl;
        int cnt = 0;
        for(int i=1; i<=n; i*=2) {
            REP(j, i) {
                cout << dat[cnt] << " ";
                cnt++;
            }
            cout << endl;
        }
        cout << "---------------------" << endl;
    }
};
/**
* 点更新区間min d=INF, f=min(a,b), g=b
* 点更新区間max d=-INF, f=max(a,b), g=b
* 点加算区間和  d=0, f=a+b, g+=b
*/

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll n;
    cin >> n;
    vector<ll> a(n);
    REP(i, n) cin >> a[i];

    auto f = [](ll a, ll b){ return min(a, b); };
    auto g = [](ll a, ll b) { return b; };
    segtree<ll> seg(n, f, g, INF);
    seg.build(a);

    ll ret = 0;
    REP(i, n) {
        // a[i]が最小になる区間
        ll lb=-1, ub=i;
        while(ub-lb>1) {
            ll mid = (lb+ub)/2;
            if(seg.query(mid, i+1) >= a[i]) ub = mid;
            else lb = mid;
        }
        ll l=ub;
        lb=i, ub=n;
        while(ub-lb>1) {
            ll mid = (lb+ub)/2;
            if(seg.query(i, mid+1) >= a[i]) lb = mid;
            else ub = mid;
        }
        ll r = lb;

        ret += (i-l+1) * (r-i+1) * a[i];
    }
    cout << ret << endl;

    return 0;
}


Measured memory_bytes: 7552