import java.util.*

fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    val b = IntArray(n)
    for ((i, c) in readLine()!!.split(' ').map(String::toInt).withIndex()) {
        b[c - 1] = i + 1
    }
    val s = TreeSet<Int>()
    s.add(0)
    s.add(n + 1)
    var ans = 0L
    for ((i, p) in b.withIndex()) {
        ans += (s.ceiling(p)!! - p).toLong() * (p - s.floor(p)!!) * (i + 1)
        s.add(p)
    }
    println(ans)
}