#include <bits/stdc++.h>
using namespace std;
//#define DEBUG_MODE
#define endl '\n'
#ifdef DEBUG_MODE
#define DEBUG(...) debug_func_mult(split_names(#__VA_ARGS__), __VA_ARGS__)
#define DEBUG_ENDL endl << flush
#define DEBUG_SEPARATOR_LINE cout<<"=================\n"
#else
#define DEBUG(...) 0
#define DEBUG_ENDL 0
#define DEBUG_SEPARATOR_LINE 0
#endif
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
#define DEBUG_ENDL_S(S) ((S).size() ? "\n" : "") << flush;

template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
template <typename T, typename U> using P = pair<T, U>;
using ll = int64_t;
using PLL = P<ll, ll>;

template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename Head, typename... Tail> const Head& var_min(const Head &head, const Tail&... tail) { return min(head, var_min(tail...)); }
template <typename Head, typename... Tail> const Head& var_max(const Head &head, const Tail&... tail) { return max(head, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }

string to_string(const string &s) { return s; }

template <typename T, typename U>
string to_string(const P<T, U> &p) {
    string ret = "(";
    ret += to_string(p.first);
    ret += ", ";
    ret += to_string(p.second);
    ret += ")";
    return move(ret);
}

template <typename T>
string to_string(const V<T> &v) {
    string ret = "{";
    for(const T &t : v) {
        ret += to_string(t);
        ret += ", ";
    }
    ret += "}";
    return move(ret);
}

template <typename T>
void debug_func(const T &t, const string &s = "") {
    if(s.size()) cout << s << " = ";
    cout << to_string(t) << DEBUG_ENDL_S(s);
}

template <typename T>
void debug_func_mult(int idx, const V<string> &names, const T &t) {
    debug_func(t, names[idx]);
}

template <typename T>
void debug_func_mult(const V<string> &names, const T &t) {
    debug_func(t, names[0]);
}

template <typename Head, typename... Tail>
void debug_func_mult(int idx, const V<string> &names, const Head &head, const Tail&... args) {
    debug_func(head, names[idx]);
    debug_func_mult(idx + 1, names, args...);
}

template <typename Head, typename... Tail>
void debug_func_mult(const V<string> &names, const Head &head, const Tail&... args) {
    debug_func(head, names[0]);
    debug_func_mult(1, names, args...);
}

V<string> split_names(string &&s) {
    replace(ALL(s), ' ', ',');
    V<string> ret;
    istringstream ss(s);
    string t;
    while(getline(ss, t, ',')) if(t.size()) ret.push_back(move(t));
    return move(ret);
}

void init_io() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(30);
}

const ll INF = 5e15;

template <typename T>
class SegmentTree {
public:
    ll size;
    vector<T> node;
    function<T(T, T)> comp;
    T identity_ele;  //comp(x, identity_ele) == x

    SegmentTree(const vector<T> &v, function<T(T, T)> comp, T identity_ele) 
        : comp(comp),
          identity_ele(identity_ele)
    {
        ll tmp = 1;
        while(tmp < v.size()) tmp = (tmp << 1);
        this->size = tmp;
        this->identity_ele = identity_ele;
        this->comp = comp;
        node.resize(2 * size - 1);
        for(ll i = 0; i < v.size(); i++) {
            node[i + size - 1] = v[i];
        }
        for(ll i = size - 2; 0 <= i; i--) {
            node[i] = comp(node[i * 2 + 1], node[i * 2 + 2]);
        }
    }

    void update(ll pos, T value) {
        pos += size - 1;
        node[pos] = value;
        while(pos > 0) {
            pos = (pos - 1) / 2;
            node[pos] = comp(node[2 * pos + 1], node[2 * pos + 2]);
        }
    }

    T query(ll a, ll b) {
        return sub_query(a, b, 0, 0, size);
    }

    T sub_query(ll a, ll b, ll k, ll l, ll r) {
        if(r <= a || b <= l) {
            return identity_ele;
        }else if(a <= l && r <= b) {
            return node[k];
        }else{
            T left = sub_query(a, b, k * 2 + 1, l, (l + r) / 2);
            T right = sub_query(a, b, k * 2 + 2, (l + r) / 2, r);
            return comp(left, right);
        }
    }
};

ll N;

V<ll> calc_left_dis(const V<ll> &A) {
    V<ll> ret(N);
    SegmentTree<ll> seg(V<ll>(N + 10, -1), [](ll a, ll b) { return max(a, b); }, -1);
    for(ll i = 0; i < N; i++) {
        ret[i] = i - seg.query(0, A[i]);
        seg.update(A[i], i);
    }
    return ret;
}

int main() {
    init_io();
    cin >> N;
    V<ll> A(N);
    for(ll &e : A) cin >> e;
    auto left = calc_left_dis(A);
    reverse(ALL(A));
    auto right = calc_left_dis(A);
    reverse(ALL(A));
    reverse(ALL(right));
    DEBUG(left, right);
    ll ans = 0;
    for(ll i = 0; i < N; i++) ans += A[i] * left[i] * right[i];
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 10592