Problem:
Score : 500 points

Problem Statement

Given are N positive integers A_1,...,A_N.

Consider positive integers B_1, ..., B_N that satisfy the following condition.

Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds.

Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.

Since the answer can be enormous, print the sum modulo (10^9 +7).

Constraints

1 \leq N \leq 10^4

1 \leq A_i \leq 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).

Sample Input 1

3
2 3 4

Sample Output 1

13

Let B_1=6, B_2=4, and B_3=3, and the condition will be satisfied.

Sample Input 2

5
12 12 12 12 12

Sample Output 2

5

We can let all B_i be 1.

Sample Input 3

3
1000000 999999 999998

Sample Output 3

996989508

Print the sum modulo (10^9+7).

Solution in Haskell (GHC 8.8.3) (judge verdict: Accepted)

{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE KindSignatures      #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE Strict              #-}
{-# LANGUAGE TypeApplications    #-}
import qualified Data.ByteString.Char8 as B
import qualified Data.IntMap.Strict    as M
import           Data.List             (mapAccumL, unfoldr)
import           Data.Proxy            (Proxy (..))
import           Data.Ratio            ((%))
import           GHC.TypeLits          (KnownNat, Nat, natVal)

newtype Mod a (n :: Nat) = Mod { unMod :: a }
  deriving (Eq, Ord)

modulo :: forall n a. (Integral a, KnownNat n) => a -> Mod a n
modulo a = Mod . mod a $ modulus @n

modulus :: forall n a. (Integral a, KnownNat n) => a
modulus = fromInteger $ natVal (Proxy @n)

instance Show a => Show (Mod a n) where
  show (Mod a) = show a

instance (Integral a, KnownNat n) => Num (Mod a n) where
  fromInteger = modulo . fromInteger

  Mod a + Mod b = modulo $ a + b
  Mod a * Mod b = modulo $ a * b

  abs    (Mod a) = modulo $ abs a
  signum (Mod a) = modulo $ signum a
  negate (Mod a) = modulo $ negate a

instance (Integral a, KnownNat n) => Enum (Mod a n) where
  succ (Mod x)
    | x == unMod (maxBound :: Mod a n) =
        errorWithoutStackTrace "Prelude.Enum.succ{Mod}: tried to take `succ' of maxBound"
    | otherwise           = Mod $ x + 1

  pred (Mod x)
    | x == unMod (minBound :: Mod a n) =
        errorWithoutStackTrace "Prelude.Enum.pred{Mod}: tried to take `pred' of minBound"
    | otherwise            = Mod $ x - 1

  toEnum = fromIntegral
  fromEnum = fromIntegral . unMod

  enumFrom     x   = enumFromTo     x maxBound
  enumFromThen x y = enumFromThenTo x y bound
    where
      bound | fromEnum y >= fromEnum x = maxBound
            | otherwise                = minBound

instance (Integral a, KnownNat n) => Bounded (Mod a n) where
  maxBound = Mod $ pred (modulus @n)
  minBound = 0

instance (Integral a, KnownNat n) => Real (Mod a n) where
  toRational (Mod a) = toInteger a % 1

instance (Integral a, KnownNat n) => Integral (Mod a n) where
  toInteger (Mod a) = toInteger a
  quotRem a b = (a * inv b, 0)

inv :: forall n a. (Integral a, KnownNat n) => Mod a n -> Mod a n
inv x = go (unMod x) (modulus @n) 1 0
  where go _ 0 u _ = modulo u
        go a b u v = go b (a - t * b) v (u - t * v)
          where t = a `div` b

type MInt = Mod Int 1000000007

main :: IO ()
main = do
  n <- readLn
  as <- readIntsLn
  print $ solve n as

solve :: Int -> [Int] -> MInt
solve _ as = sum $ [l `div` (modulo a :: MInt) | a <- as]
  where l = product [(modulo p :: MInt) ^ f | (p, f) <- M.assocs pfs]
        pfs = M.fromListWith max $ concatMap primeFactorize as

primeFactorize :: Int -> [(Int, Int)]
primeFactorize n
  | p == 1 = pfs'
  | otherwise = (p, 1) : pfs'
  where pfs' = filter (\(_, f) -> f /= 0) pfs
        (p, pfs) = mapAccumL factor n ps
        ps = [2 .. x]
        x = floor $ (sqrt (fromIntegral n) :: Double)

factor :: Int -> Int -> (Int, (Int, Int))
factor n p = go n 0
  where go m f
          | y == 0    = go x $ f + 1
          | otherwise = (m, (p, f))
          where (x, y) = divMod m p

readIntsLn :: IO [Int]
readIntsLn = unfoldr (B.readInt . B.dropWhile (== ' ')) <$> B.getLine