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 Kotlin (1.3.71) (judge verdict: Time Limit Exceeded)

import java.io.*
import java.util.*
import java.lang.*
import java.lang.Math.*


class LongMod(var num: Long, val MOD: Long = 1_000_000_007L) {
    init {
        this.num = (this.num%this.MOD + this.MOD)%this.MOD
    }
    constructor(numStr: String, MOD: Long = 1e9.toLong()+7) :
            this(0L, MOD) {
        if (numStr.isEmpty()) return
        var rv = numStr.reversed()
        var d = 1L
        rv.forEach{ c ->
            this.num += (c - '0') * d
            this.num %= MOD
            d *= 10
            d %= MOD
        }
    }
    override operator fun equals(other: Any?): Boolean {
        if (other !is LongMod) return false
        return this.num % this.MOD == other.num % this.MOD
    }
    operator fun unaryMinus(): LongMod {
        return LongMod(-this.num)
    }
    private operator fun plusAssign(other: LongMod): Unit {
        if ((this.num + other.num) >= this.MOD) {
            this.num -= this.MOD
        }
        this.num += other.num
    }
    private operator fun minusAssign(other: LongMod): Unit {
        if ((this.num + this.MOD - other.num) >= this.MOD) {
            this.num -= this.MOD
        }
        this.num += this.MOD - other.num
    }
    private operator fun timesAssign(other: LongMod): Unit {
        this.num *= other.num
        this.num %= this.MOD
    }
    operator fun plus(other: LongMod): LongMod {
        val res = LongMod(this.num, this.MOD)
        res += other
        return res
    }
    operator fun minus(other: LongMod): LongMod {
        val res = LongMod(this.num, this.MOD)
        res -= other
        return res
    }
    operator fun times(other: LongMod): LongMod {
        val res = LongMod(this.num, this.MOD)
        res *= other
        return res
    }
    fun pow(n: Long): LongMod {
        if (n == 0L) return LongMod(1L, this.MOD)
        var x = this.pow(n / 2)
        x.timesAssign(x)
        if (n % 2 == 1L) x.timesAssign(this)
        return x
    }
    fun inv(): LongMod {
        return this.pow(this.MOD - 2)
    }
    operator fun divAssign(other: LongMod): Unit {
        this *= other.inv()
    }
    operator fun div(other: LongMod): LongMod {
        val res = LongMod(this.num, this.MOD)
        res /= other
        return res
    }
}

fun sievePrimes(N: Int): BooleanArray {
    if (N <= 1) return BooleanArray(N+1){false}
    var fc = IntArray(N+1){0}
    fc[0] = -1
    fc[1] = -1
    for (i in 2..N) {
        if (fc[i] != 0) continue
        fc[i] = i
        //for(j in i*2..N step i) {
        val il = i.toLong()
        for (jl in il*il..N step il) {
            val j = jl.toInt()

            if (fc[j] == 0) fc[j] = i
        }
    }
    var isPrime = BooleanArray(N+1){false}
    for (i in 2..N) {
        if (fc[i] == i) isPrime[i] = true
    }
    return isPrime
}

fun primeFactorize(primes: LongArray, N: Long): Map<Long, Long> {
    if (N <= 1L) return mapOf()
    val fact = mutableMapOf<Long, Long>()
    var n = N
    for (i in primes) {
        if (i * i > n) break
        while (n%i == 0L) {
            fact[i] = (fact[i] ?: 0) + 1
            n /= i
        }
    }
    if (n != 1L) {
        fact[n] = (fact[n] ?: 0) + 1
    }
    return fact
}


fun solve() {
    val N = rd.readInt()
    val A = rd.readLongArray()
    val RTAMAX = 1_010

    val primes = ArrayList<Long>()
    sievePrimes(RTAMAX).forEachIndexed{i, v->
        if (v) primes.add(i.toLong())
    }

    val primesLA = primes.toLongArray()

    var pfA = ArrayList<Map<Long, Long>>()
    val pfL = mutableMapOf<Long, Long>()
    for (i in 0 until N) {
        val pfa = primeFactorize(primesLA, A[i])
        pfA.add(pfa)
        for ((k,v) in pfa) {
            pfL[k] = max(pfL[k]?:0, v)
        }
    }

    var ans = LongMod(0L)
    for (i in 0 until N) {
        var b = LongMod(1L)
        val pfa = pfA[i]

        for ((k,v) in pfL) {
            b *= LongMod(k).pow(v - (pfa[k] ?: 0))
        }
        ans += b
    }


    print(ans.num)
}


//val rd = debug.Reader("abc", "152", "e", "sample-1")
val rd = Reader()


fun main(args: Array<String>) {
    try {
        var exception: Throwable? = null
        val sth = Thread(null, SolveThread(), "solveThread", 128 * 1024 * 1024)
        sth.setUncaughtExceptionHandler { _, e -> exception = e }
        sth.start(); sth.join()
        if (exception != null) throw exception!!
    } catch (e: Exception) { throw e }
}
const val MOD = 1_000_000_007L; const val INF = Int.MAX_VALUE/2; const val LINF = Long.MAX_VALUE/2
const val EPS = 1e-9; fun DBLEQ(a: Double, b: Double): Boolean {return abs(a - b) < EPS}
data class IntPair(val first: Int, val second: Int)
data class To(val idx: Int, val cost: Long)
fun <T>edge(V: Int): Array<ArrayList<T>> { return Array(V){ArrayList<T>()} }
val errPrintln: (Any) -> Unit = { msg -> System.err.println(msg) }
fun rAssert(exp: Boolean, lazyMsg: () -> String = {""}) {
    if (!exp) { val msg = lazyMsg(); errPrintln("EAssert Error: $msg"); throw Error(msg) }
}
fun tAssert(exp: Boolean, lazyMsg: () -> String = {""}) {
    if (!exp) { val msg = lazyMsg(); errPrintln("TAssert Error: $msg"); Thread.sleep(3000) }
}
class SolveThread : Runnable {
    override fun run() { solve() }
}
class Reader {
    private val br = BufferedReader(InputStreamReader(System.`in`))
    val readString: () -> String = { br.readLine() }
    val readInt: () -> Int = { br.readLine().toInt() }
    val readLong: () -> Long = { br.readLine().toLong() }
    val readIntArray: () -> IntArray = { br.readLine().split(' ').map { it.toInt() }.toIntArray() }
    val readLongArray: () -> LongArray = { br.readLine().split(' ').map { it.toLong() }.toLongArray() }
    val readListInt: () -> List<Int> = { br.readLine().split(' ').map { it.toInt() } }
    val readListLong: () -> List<Long> = { br.readLine().split(' ').map { it.toLong() } }
}