import Control.Monad
import Control.Applicative
import Data.List

taka :: [Int] -> Int
taka xs = sum $ map directionaryOrder (p5 ( length xs ) xs (kuwa xs 1)) 

p5 :: Int -> [Int] -> [Int] -> [[Int]]
p5 _ [] _ = [[]]
p5 n (x:xs) ys
    | x == 0 = do
        x <- ys
        map (x:) (p5 n xs ( delete x ys ))
    | x /= 0 = do
        map (x:) (p5 n xs ys)

kuwa :: [Int] -> Int -> [Int]
kuwa [] _ = []
kuwa (x:xs) n
    | x == 0 = (n:) (kuwa xs (n+1))
    | x /= 0 = kuwa xs (n+1)
    
directionaryOrder :: (Ord a) => [a] -> Int
directionaryOrder [] = 1
directionaryOrder all@(x:xs) = (order (sort all) x) * factorial (length xs) + directionaryOrder (xs)

order :: (Eq a) => [a] -> a -> Int
order xs y = iter xs y 0
    where
        iter (x:xs) y acc
            | x == y = acc
            | x /= y = iter xs y (acc + 1)

factorial :: Integral a => a -> a
factorial 0 = 1
factorial n = n * factorial (n - 1)

main = do
    n' <- getLine
    p' <- getLine
    print $ taka $ map (\x -> (read x :: Int)) (words p')
