#include <bits/stdc++.h>
using namespace std;
struct Fast {Fast(){std::cin.tie(0);ios::sync_with_stdio(false);cout.precision(10);}} fast;

/* define */
#define FOR(I,X,Y) for(long long (I)=(X);(I)<(Y);(I)++)
#define REP(I,X,Y) for(long long (I)=(Y)-1;(I)>=(X);(I)--)
#define ALL(X) (X).begin(),(X).end()
#define pb push_back
#define COUNT(V,X) (upper_bound((V).begin(),(V).end(),X)-lower_bound((V).begin(),(V).end(),X))
#define debug(x) cerr<<#x<<':'<<x<<endl;
#define DEBUG(v) cerr<<#v<<':';for(auto xXx:v)cerr<<xXx<<' ';cerr<<endl;
#define Yes(X) if(X){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
#define YES(X) if(X){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define ctoi(C) (C-'0')

/* alias */
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vii = vector<vector<int>>;
using vl = vector<long long>;
using vll = vector<vector<long long>>;
using pi = pair<int,int>;
using pl = pair<long long,long long>;
template<typename T> using PQ = priority_queue<T>;
template<typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;

/* const */
const long long dx[] = {1,0,-1,0};
const long long dy[] = {0,1,0,-1};
const long long dx8[] = {1,1,0,-1,-1,-1,0,1};
const long long dy8[] = {0,1,1,1,0,-1,-1,-1};
const long long dx9[] = {1,1,0,-1,-1,-1,0,1,0};
const int INF = 1000000007;
const long long LINF = 1000000000000000007;

/* func */
template <typename T> inline bool chmin(T& a, const T& b) {if (a > b) a = b; return a > b;}
template <typename T> inline bool chmax(T& a, const T& b) {if (a < b) a = b; return a < b;}
long long max(long long x,int y){return max(x,(long long)y);}
long long max(int x,long long y){return max((long long)x,y);}
long long min(long long x,int y){return min(x,(long long)y);}
long long min(int x,long long y){return min((long long)x,y);}

/* liblary */

struct SparseTable{
    vector<vector<long long>> table_min;
    vector<vector<long long>> table_max;
    vector<long long> log_table;
    SparseTable(const vector<long long> &A) : table_min(),table_max(){
        log_table.resize(A.size()+1);
        log_table[1] = 0;
        for(long long i = 2;i <= A.size();i++){
            log_table[i] = log_table[i/2] + 1;
        }
        table_min.resize(A.size());
        fill(table_min.begin(),table_min.end(),vector<long long>(log_table[A.size()]+1));
        for(long long i = 0;i < A.size();i++)table_min[i][0] = A[i];
        for(long long i = 1;(1 << i) <= A.size();i++){
            for(long long j = 0;j+(1<<i) <= A.size();j++){
                table_min[j][i] = min(table_min[j][i-1],table_min[j+(1<<(i-1))][i-1]);
            }
        }
        table_max.resize(A.size());
        fill(table_max.begin(),table_max.end(),vector<long long>(log_table[A.size()]+1));
        for(long long i = 0;i < A.size();i++)table_max[i][0] = A[i];
        for(long long i = 1;(1 << i) <= A.size();i++){
            for(long long j = 0;j+(1<<i) <= A.size();j++){
                table_max[j][i] = max(table_max[j][i-1],table_max[j+(1<<(i-1))][i-1]);
            }
        }
    }
    long long query_min(long long i,long long j){
        return min(table_min[i][log_table[j-i+1]],table_min[j-(1 << log_table[j-i+1])+1][log_table[j-i+1]]);
    }
    long long query_max(long long i,long long j){
        return max(table_max[i][log_table[j-i+1]],table_max[j-(1 << log_table[j-i+1])+1][log_table[j-i+1]]);
    }
};

/* main */

signed main(){
    ll N;
    cin >> N;
    vector<ll> a(N+2,0);
    FOR(i,0,N)cin >> a[i+1];
    SparseTable st(a);
    ll ans = 0;
    FOR(i,1,N+1){
        ll lower_imin = 0;
        ll lower_imax = i;
        while(lower_imax - lower_imin > 1){
            ll imid = (lower_imin+lower_imax)/2;
            if(st.query_min(imid,i) == a[i])lower_imax = imid;
            else lower_imin = imid;
        }
        ll upper_imin = i;
        ll upper_imax = N+1;
        while(upper_imax - upper_imin > 1){
            ll imid = (upper_imin+upper_imax)/2;
            if(st.query_min(i,imid) == a[i])upper_imin = imid;
            else upper_imax = imid;
        }
        ans += a[i]*(i-lower_imax+1)*(upper_imin-i+1);
    }
    cout << ans << endl;
}

Measured memory_bytes: 75264