import Control.Applicative ((<$>)) import Control.Arrow ((&&&)) import Data.List (genericLength, group, sort) import qualified Data.Map.Strict as Map type ExpTable a = Map.Map a [a] type FactorNumTable a = Map.Map a a mp :: Integral a => a mp = 10 ^ 9 + 7 modexp :: Integral a => a -> a -> a -> a modexp b ex m | ex == 0 = 1 | even ex = flip mod m $ square $ flip mod m $ modexp b (div ex 2) m | otherwise = flip mod m $ mod b m * (flip mod m $ modexp b (ex - 1) m) where square x = x * x -- Based on . freq :: (Integral a) => [a] -> [(a, [a])] freq = map (head &&& (:[]) . genericLength) . group . sort -- Based on . primes2 :: Integral a => [a] primes2 = map fromIntegral primes2' where primes2' = [2, 3, 5] ++ sieve2 5 7 (drop 2 primes2') sieve2 m s (p : ps) = [n | n <- ns, gcd m n == 1] ++ sieve2 (m * p) (p * p) ps where ns = [x + y | x <- [s, s + 6 .. p * p - 2], y <- [0, 4]] -- Based on . factorize2 :: Integral a => a -> [a] factorize2 1 = [] factorize2 n = factorize2' n primes2 where factorize2' n ps@(p : pr) | p * p > n = [n] | m == 0 = p : factorize2' d ps | otherwise = factorize2' n pr where (d, m) = divMod n p calcExponents :: Integral a => [a] -> ExpTable a calcExponents = Map.unionsWith (++) . map ce where ce = Map.fromAscList . freq . factorize2 factorNumFromExps :: Integral a => Int -> ExpTable a -> FactorNumTable a factorNumFromExps k = Map.map (sum . take k . reverse . sort) lcmFromFactorNum :: Integral a => a -> FactorNumTable a -> a lcmFromFactorNum m = flip mod m . product . map f . Map.toAscList where f (p, n) = modexp p n m solve :: Integral a => Int -> [a] -> a solve k xs = lcmFromFactorNum mp $ factorNumFromExps k $ calcExponents xs main :: IO () main = do [_, k] <- map read . words <$> getLine xs <- map read . words <$> getLine print $ solve k xs