#include <bits/stdc++.h>
#include <boost/algorithm/string/join.hpp>
#include <boost/range/irange.hpp>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using boost::irange;
using boost::algorithm::join;

typedef long long ll;
typedef unsigned long long ull;
//typedef long double ld;

typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double,double> pdd;
typedef set<int> si;
typedef set<ll> sll;
typedef vector<int> vi;
typedef vector<pair<int, int > > vii;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef vector<vll> vvll;

constexpr ll MOD = 1e9+7;
template<typename T>constexpr auto inf=numeric_limits<T>::max()/4;
constexpr int INF=inf<int>;
constexpr ll LINF=inf<ll>;

#define Sort(x) sort(begin(x),end(x))
#define Reverse(x) reverse(begin(x),end(x))
#define ABS(a,b) ((a)<(b)?(b)-(a):(a)-(b))
#define nbit(n) (1ull<<(n))
#define _ol3(_1,_2,_3,name,...)name
#define _rep(i,n)_repi(i,0,n)
#define _repi(i,a,b)for(int i=a,i##_l=(b);i<i##_l;++i)
#define REP(...) _ol3(__VA_ARGS__,_repi,_rep,)(__VA_ARGS__)
#define REPR(i,n) for(int i=n-1;i>=0;--i)
#define REPA(i,v) REP(i,(v).size())

// sort(begin(x), end(x), greater<ll>());
// lower_bound(a.begin(), a.end(), val): a[i] ≥ valである最左の位置
// upper_bound(a.begin(), a.end(), val): a[i] > valである最左の位置

// Returns minimum of a and b.
// If a is less b, a is set to b.
template<typename T>
T chmin(T& a, T b) {
  if (a > b) {
    a = b;
  }
  return a;
}

// Returns maximum of a and b.
// If a is less b, a is set to b.
template<typename T>
T chmax(T& a, T b) {
  if (a < b) {
    a = b;
  }
  return a;
}

template <typename T>
void dumpContents(const T& v, const string& msg)
{
  cerr << "### " << msg << " ###\n";
  for (const auto& x : v) {
    cerr << x << " ";
  }
  cerr << endl;
}

struct before_main_function {
  before_main_function() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout<<setprecision(20)<<fixed;
    #define endl "\n"
  }
} before_main_function;

template <typename T>
class SegmentTree {
private:
  using F = function<T(T,T)>; // min or max

  F f;
  T ti; // 単位元 (INF for min or -INF for max)

  int n;
  vector<T> dat;

  // Find a minimum in [a,b).
  // A node k corresponds to a range [l,r) in the segment tree.
  // So, the initial query should be query(a, b, 0, 0, n)
  T query(const int a, const int b, const int k, const int l, const int r) {
     // 1) No cross between [a,b) and [l, r)
     if (r<=a || b<=l) {
       return ti;
     }

     // 2) [a,b) includes [l ,r)
     if (a <= l && r <= b) return dat[k];

     // 3) otherwise
     const T vl = query(a, b, k*2+1, l, (l+r)/2);
     const T vr = query(a, b, k*2+2, (l+r)/2, r);
     return f(vl, vr);
  }

public:
  SegmentTree(F _f, T _ti)
    : f(_f), ti(_ti) {}

  void init(int n_) {
    // Set n to power of 2
    n = 1;
    while (n < n_) n *= 2;

    dat.resize(2*n-1);
    fill(begin(dat), end(dat), ti);
  }

  void build(const vector<T>& v) {
    const int n_ = v.size();
    init(n_);

    for(int i=0; i<n_; i++) 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]);
  }

  // Update the value of the k-th node (zero indexed) to a.
  void update(int k, T a) {
    k += n-1;
    dat[k] = a;
    // Climbing up the tree
    while (k>0) {
      k = (k-1)/2;
      dat[k] = f(dat[k*2+1], dat[k*2+2]);
    }
  }

  // Find a minimum or maximum in [a,b).
  T query(const int a, const int b) {
    return query(a, b, 0, 0, n);
  }

};

// ========== end of template ==========
int N;
vi A;
vii range;

int main(int argc, char** argv) {
  cin >> N;
  A.resize(N+2);
  A[0] = -INF; // dummy
  A[N+1] = -INF; // dummy

  for (int i=1; i<=N; i++) {
    int a;
    cin >> a;

    A[i] = a;
  }

  // [a,b) = range[x]
  // xは、Aのindex [a,b)で最小
  range.resize(N+1);

  {
    // Range Minimum Query
    SegmentTree<int> rmq(
            [](int a, int b) {
              return max(a, b);
            },
            -INF);
    rmq.init(N+10);

    for (int i=1; i<=N; i++) {
      int a = A[i];
      int k = rmq.query(1, a);
      range[a].first = k<0 ? 1 : k+1;
      rmq.update(a, i);
//cerr << a << ", k " << k << endl;
    }
  }
  {
    // Range Minimum Query
    SegmentTree<int> rmq(
            [](int a, int b) {
              return min(a, b);
            },
            INF);
    rmq.init(N+10);

    for (int i=N; i>=1; i--) {
      int a = A[i];
      int k = rmq.query(1, a);
      range[a].second = k>N ? N+1 : k;
      rmq.update(a, i);
//cerr << a << ", k " << k << endl;
    }
  }

//for (int i=1; i<=N; i++) {
//  int a = A[i];
//  auto p = range[a];
//  cerr << "a " << a << ": [" << p.first << ", " << p.second << ")" << endl;
//}

  ll ret = 0;
  for (int i=1; i<=N; i++) {
    int a = A[i];
    int s = range[a].first;
    assert(1<=s && s<=i);
    int e = range[a].second;
    assert(i<=e && e<=N+1);

    ret += ll(a)*ll(i-s+1)*ll(e-i);
  }

  cout << ret << endl;

  return 0;
}


Measured memory_bytes: 4608