{-# LANGUAGE MultiWayIf #-}
import qualified Data.Vector as V
import qualified Data.ByteString.Char8 as BS
import Data.Maybe

count :: Int -> Int -> Int
count n i = sum [ minimum [i, l, n-i+1, n-l+1] | l <- [1..n] ]

solve :: V.Vector Int -> Int
solve v = if
  | v == V.fromList [1..n] -> n * (n+1) * (n+2) `div` 6
  | V.null v -> 0
  | otherwise -> mv * count n (m+1) + solve l + solve (V.tail r)
  where
    n = V.length v
    m = V.minIndex v
    mv = v V.! m
    (l, r) = V.splitAt m v

main :: IO ()
main = do
  getLine
  a <- V.fromList . map (fst . fromJust . BS.readInt) . BS.words <$> BS.getLine
  print $ solve a
