import Data.List

taka :: [Int] -> Int
taka xs = mod (sum $ map directionaryOrder (p5 (length xs) xs ([1..(length xs)] \\ (kuwa xs)))) (7 + 10 ^ 9)

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]
kuwa [] = []
kuwa (x:xs)
    | x == 0 = kuwa xs
    | x /= 0 = (x:) (kuwa xs)
    
directionaryOrder :: (Ord a) => [a] -> Int
directionaryOrder xs = iter xs 1
    where
        iter [] acc = acc
        iter all@(x:xs) acc = iter (xs) (acc + (order (sort all) x) * factorial (length 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 n = iter n 1
    where
        iter 0 acc = acc
        iter n acc = iter (n-1) (n*acc)

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