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

void main() {
    int n; scan(n);
    auto a = readln.split.to!(int[]);

    auto ans = new long[](n);
    ans[] = a.dup.to!(long[]);

    auto st = Stack!(int)(n);

    foreach (i ; 0 .. n) {
        while (!st.empty && a[st.front] > a[i]) {
            st.removeFront();
        }
        if (st.empty) {
            ans[i] *= i + 1;
        }
        else {
            ans[i] *= (i - st.front);
        }
        st.insert(i);
    }

    debug {
        writeln(ans);
    }

    st.clear();

    foreach_reverse (i ; 0 .. n) {
        while (!st.empty && a[st.front] > a[i]) {
            st.removeFront();
        }
        if (st.empty) {
            ans[i] *= (n - i);
        }
        else {
            ans[i] *= (st.front - i);
        }
        st.insert(i);
    }

    writeln(ans.sum());
}



struct Stack(T) {
    private {
        size_t cap, top;
        T[] data;
    }

    this (int n) in {
        assert(n > 0, "The capacity of a stack must be a positive integer.");
    }
    body {
        cap = n;
        data = new T[](cap);
    }

    void clear() {
        top = 0;
    }

    bool empty() {
        return !top;
    }

    bool full() {
        return top == cap;
    }

    size_t length() {
        return top;
    }

    T front() in {
        assert(!empty, "The stack is empty.");
    }
    body {
        return data[top - 1];
    }

    void removeFront() in {
        assert(!empty, "The stack is empty.");
    }
    body {
        top--;
    }

    alias popFront = removeFront;

    void insertFront(T x) in {
        assert(!full, "The stack is full.");
    }
    body {
        data[top++] = x;
    }

    alias insert = insertFront;

    T[] array() {
        import std.algorithm.mutation : reverse;

        T[] arr = data[0 .. cap].dup;
        arr.reverse();
        return arr;
    }

    string toString() {
        import std.format : format;
        import std.range : retro;

        return format("[%(%s, %)]", data[0 .. top].retro());
    }
}



void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    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);
        }
    }
}
