import qualified Data.IntSet as S
import qualified Data.ByteString.Char8 as BS
import Data.Maybe
import Data.List

solve :: Int -> S.IntSet -> [(Int, Int)] -> Int
solve _ _ [] = 0
solve n s ((i, x):is) = (i-l)*(r-i)*x + solve n (i `S.insert` s) is
  where
    l = fromMaybe 0 $ S.lookupLT i s
    r = fromMaybe (n+1) $ S.lookupGT i s

main :: IO ()
main = do
  n <- readLn
  a <- sortOn snd . zip [1..] . map (fst . fromJust . BS.readInt) . BS.words <$> BS.getLine
  print $ solve n S.empty a