import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); } bool DEBUG = 0; 
void log(A ...)(lazy A a){ if(DEBUG) print(a); }
void print(){ writeln(""); } void print(T)(T t){ writeln(t); } void print(T, A ...)(T t, A a){ write(t, " "), print(a); }
string unsplit(T)(T xs){ return xs.array.to!(string[]).join(" "); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; } T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
T lowerTo(T)(ref T x, T y){ if(x > y) x = y; return x; } T raiseTo(T)(ref T x, T y){ if(x < y) x = y; return x; }

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
    int n = scan!int;
    long[] as = scan!long(n);

    auto psta = new Stack!S;
    long[] xs = new long[](n); // xs[i] は i の左方に続く as[i] より大きい数の最終位置
    foreach(i, a; as){
        while(! psta.isEmpty && psta.peek.value > a) psta.pop;
        if(psta.isEmpty) xs[i] = 0;
        else xs[i] = psta.peek.pos + 1;
        psta ~= S(i, a);
    }
    log("xs:", xs);

    auto qsta = new Stack!S;
    long[] ys = new long[](n); // ys[i] は i の右方で初めて as[i] より小さい数の位置
    foreach_reverse(i, a; as){
        while(! qsta.isEmpty && qsta.peek.value > a) qsta.pop;
        if(qsta.isEmpty) ys[i] = n;
        else ys[i] = qsta.peek.pos;
        qsta ~= S(i, a);
    }
    log("ys:", ys);

    long ans;
    foreach(i; 0 .. n) ans += as[i] * (i - xs[i] + 1) * (ys[i] - i);

    ans.writeln;
}
struct S{
    long pos, value;
}

// ----- スタック -----
class Stack(T){
	private T[] xs;
	private uint j; // j : 次に書き込む位置
	this(){	}
	this(T[] xs){ this.xs = xs; j = xs.length.to!uint; }
	uint length(){ return j; }
	bool isEmpty(){ return j == 0; }
	void push(T x){
		while(j + 1 >= xs.length) xs.length = xs.length * 2 + 1;
		xs[j ++] = x;
	}
    Stack opOpAssign(string op)(T x){
        if(op == "~"){ push(x); return this; }
        assert(0, "Operator " ~ op ~ "= not implemented");
    }
	T pop(){ assert(j > 0); return xs[-- j]; }
	T peek(){ assert(j > 0); return xs[j - 1]; }
	T opIndex(uint li){ assert(j > 0 && j - 1 >= li); return xs[j - 1 - li]; }
	static Stack!T opCall(){ return new Stack!T; }
	static Stack!T opCall(T[] xs){ return new Stack!T(xs); }
	T[] array(){ return xs[0 .. j]; }
	override string toString(){ return array.to!string; }
}
