//satyaki3794
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define MOD (1000000007LL)
#define LEFT(n) (2*(n))
#define RIGHT(n) (2*(n)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
ll pwr(ll base, ll p, ll mod = MOD){
ll ans = 1;while(p){if(p&1)ans=(ans*base)%mod;base=(base*base)%mod;p/=2;}return ans;
}
ll gcd(ll a, ll b){
if(b == 0) return a;
return gcd(b, a%b);
}
int n, right_low[200005];
ll DP[200005], arr[200005];
int main(){
ios_base::sync_with_stdio(0);
cin>>n;
for(int i=1;i<=n;i++)
cin>>arr[i];
stack<int> st;
right_low[n] = n+1;
st.push(n);
for(int i=n-1;i>=1;i--){
while(!st.empty() && arr[st.top()] >= arr[i])
st.pop();
if(st.empty()) right_low[i] = n+1;
else right_low[i] = st.top();
st.push(i);
}
// for(int i=1;i<=n;i++) cout<<right_low[i]<<" ";cout<<endl;
DP[n] = arr[n];
for(int i=n-1;i>=1;i--){
DP[i] = (right_low[i]-i)*arr[i] + DP[right_low[i]];
}
ll ans = 0;
ans = accumulate(DP+1, DP+n+1, 0LL);
cout<<ans;
return 0;
}
Ly9zYXR5YWtpMzc5NAojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBmZiBmaXJzdAojZGVmaW5lIHNzIHNlY29uZAojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIE1PRCAoMTAwMDAwMDAwN0xMKQojZGVmaW5lIExFRlQobikgKDIqKG4pKQojZGVmaW5lIFJJR0hUKG4pICgyKihuKSsxKQogCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgbG9uZyBsb25nIGxsOwp0eXBlZGVmIHVuc2lnbmVkIGxvbmcgbG9uZyB1bGw7CnR5cGVkZWYgcGFpcjxpbnQsIGludD4gaWk7CnR5cGVkZWYgcGFpcjxpbnQsIGlpPiBpaWk7CiAKbGwgcHdyKG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