//---------- begin SegmentTree Point update Range query ----------
mod segment_tree {
    pub struct PURQ<T: Clone, F: Fn(T, T) -> T> {
        n: usize,
        a: Vec<T>,
        id: T,
        op: F,
    }
    #[allow(dead_code)]
    impl<T: Clone, F: Fn(T, T) -> T> PURQ<T, F> {
        pub fn new(n: usize, id: T, op: F) -> PURQ<T, F> {
            let mut k = 1;
            while k < n {
                k *= 2;
            }
            PURQ {
                n: k,
                a: vec![id.clone(); 2 * k],
                id: id,
                op: op,
            }
        }
        pub fn update(&mut self, x: usize, v: T) {
            let mut k = self.n + x;
            let a = &mut self.a;
            a[k] = v;
            k >>= 1;
            while k > 0 {
                a[k] = (self.op)(a[2 * k].clone(), a[2 * k + 1].clone());
                k >>= 1;
            }
        }
        pub fn update_tmp(&mut self, x: usize, v: T) {
            self.a[x + self.n] = v;
        }
        pub fn update_all(&mut self) {
            for k in (1..(self.n)).rev() {
                self.a[k] = (self.op)(self.a[2 * k].clone(), self.a[2 * k + 1].clone());
            }
        }
        pub fn find(&self, mut l: usize, mut r: usize) -> T {
            let mut p = self.id.clone();
            let mut q = self.id.clone();
            l += self.n;
            r += self.n;
            while l < r {
                if (l & 1) == 1 {
                    p = (self.op)(p, self.a[l].clone());
                    l += 1;
                }
                if (r & 1) == 1 {
                    r -= 1;
                    q = (self.op)(self.a[r].clone(), q);
                }
                l >>= 1;
                r >>= 1;
            }
            (self.op)(p, q)
        }
    }
}
//---------- end SegmentTree Point update Range query ----------
use std::io::Read;

fn run() {
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    let n: usize = it.next().unwrap().parse().unwrap();
    let p: Vec<u64> = (0..n).map(|_| it.next().unwrap().parse().unwrap()).collect();
    type T = ((u64, usize), (u64, usize));
    let f = |a: T, b: T| {
        let (x, y) = a;
        let (z, w) = b;
        if x.0 > z.0 {
            (x, if y.0 > z.0 {y} else {z})
        } else {
            (z, if w.0 > x.0 {w} else {x})
        }
    };
    let mut s = segment_tree::PURQ::new(n, ((0, n + 1), (0, n + 1)), f);
    for (i, p) in p.iter().enumerate() {
        s.update_tmp(i, ((*p, i), (0, n + 1)));
    }
    s.update_all();
    let mut ans = 0;
    let mut used = std::collections::HashSet::new();
    let mut stack = vec![];
    stack.push((0, n));
    while let Some((l, r)) = stack.pop() {
        if l + 1 >= r || used.contains(&(l, r)) {
            continue;
        }
        used.insert((l, r));
        let ((_, mut a), (v, mut b)) = s.find(l, r);
        if a > b {
            std::mem::swap(&mut a, &mut b);
        }
        ans += v * ((a + 1 - l) * (r - b)) as u64;
        stack.push((l, b));
        stack.push((a + 1, r));
    }
    println!("{}", ans);
}

fn main() {
    run();
}


Measured memory_bytes: 27416