// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
static immutable MOD=10^^9+7;alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop;
// dfmt on

void main()
{
    long N = lread();
    auto A = aryread();
    auto idx = new long[](N + 1);
    foreach (i; 0 .. N)
    {
        idx[A[i]] = i;
    }
    auto rbt = new RedBlackTree!long();
    rbt.insert(-1);
    rbt.insert(N);
    long ans;
    foreach (x; 0 .. N)
    {
        long i = idx[x + 1];
        long l = rbt.lowerBound(i).back;
        long r = rbt.upperBound(i).front;
        ans += (r - i) * (i - l) * (x + 1);
        rbt.insert(i);
        dprint(x, i, r, l, (r - i), (i - l));
    }
    writeln(ans);
}
