import Data.List

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

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

kuwa :: [Int] -> [Int]
kuwa xs = filter (\x -> not (elem x xs)) [1..(length xs)]

directionaryOrder :: [Int] -> Int
directionaryOrder xs = iter xs (sort xs) 1
    where
        iter [] _ acc = mod acc (7 + 10 ^ 9)
        iter (x:xs) all acc = iter (xs) (delete x all) (acc + (order all x) * factorial (length xs))

order :: [Int] -> Int -> 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')
