import java.util.TreeSet
fun main(args: Array<String>) {
    val N = nextInt()
    val A = listOfInt()
    val B = LongArray(N)
    for ((i, a) in A.withIndex()) {
        B[a - 1] = i.toLong() + 1
    }
    val S = TreeSet<Long>()
    S.add(0L)
    S.add(N.toLong() + 1)
    var ans = 0L
    for ((i, p) in B.withIndex()) {
        ans += (S.ceiling(p)!! - p) * (p - S.floor(p)!!) * (i + 1)
        S.add(p)
    }
    println(ans)
}
fun next() = readLine()!!
fun nextInt() = Integer.parseInt(next())
fun listOfString() = next().split(" ")
fun listOfInt() = listOfString().map { it.toInt() }