import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;
import std.numeric;

void main() {
    int n;
    int[] a;

    scan(n);
    a = readln.split.to!(int[]);

    a = [0] ~ a ~ [0];

    auto lb = new int[](n + 2), rb = new int[](n + 2);
    auto st = Stack!(int)(n);
    st.push(0);

    foreach (i ; 1 .. n + 1) {
        while (!st.empty && a[st.top] > a[i]) {
            st.pop();
        }

        lb[i] = st.top;
        st.push(i);
    }

    st.clear();
    st.push(n + 1);

    foreach_reverse (i ; 1 .. n + 1) {
        while (!st.empty && a[st.top] >= a[i]) {
            st.pop();
        }

        rb[i] = st.top;
        st.push(i);
    }

    long ans;

    foreach (i ; 1 .. n + 1) {
        ans += 1L * (i - lb[i]) * (rb[i] - i) * a[i];
    }

    writeln(ans);
}



struct Stack(T) {
private:
    int N, peek;
    T[] data;

public:
    this(int size) 
    {
        N = size;
        data = new T[](N);
    }

    bool empty() @property
    {
        return peek == 0;
    }

    bool full() @property
    {
        return peek == N;
    }

    void push(T x) @property
    {
        assert(!full);
        data[peek++] = x;
    }

    void pop() @property
    {
        assert(!empty);
        --peek;
    }

    T top() @property
    {
        return data[peek - 1];
    }

    void clear() @property
    {
        peek = 0;
    }

    int length() @property
    {
        return peek;
    }

}



void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}



void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}