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.0.0) (judge verdict: Accepted)

import java.util.*

fun main(args: Array<String>) {
    readLine()!!.toInt()
    val A = readLine()!!.split(" ").map(String::toInt)
    val lcm = Sieve.lcm(A)
    val ans = A.map { i -> lcm / MInt(i) }.reduce { acc, i -> acc + i }
    println(ans)
}

object Sieve {
    private val N = Math.sqrt(1000000.0).toInt()
    private val primes: Array<Int>

    init {
        val que = ArrayDeque<Int>()
        val sieve = BooleanArray(N+1) { true }
        sieve[0] = false
        sieve[1] = false
        for (i in 2..N) {
            if (!sieve[i]) continue
            que.push(i)
            for (j in (i*2)..N step i) {
                sieve[j] = false
            }
        }
        primes = que.toTypedArray()
    }

    fun lcm(array: List<Int>): MInt {
        val map = HashMap<Int, Int>()
        for (n in array) {
            fact(n).forEach { e ->
                val ex = map[e.key] ?: 0
                map[e.key] = Math.max(ex, e.value)
            }
        }
        var mint = MInt(1)
        map.forEach { e ->
            mint = mint * MInt(e.key).pow(e.value)
        }
        return mint
    }

    fun fact(n: Int): HashMap<Int, Int> {
        val map = HashMap<Int, Int>()
        var tmp = n
        for (prime in primes) {
            if (tmp % prime == 0) {
                var cnt = 0
                while (tmp % prime == 0) {
                    tmp /= prime
                    cnt++
                }
                map[prime] = cnt
            }
        }
        if (tmp != 1) {
            map[tmp] = 1
        }
        return map
    }
}

class MInt {
    private val MOD = 1000000007L
    var v: Long

    constructor(v: Long) {
        this.v = v % MOD
    }

    constructor(v: Int) {
        this.v = v.toLong() % MOD
    }

    operator fun plus(b: MInt): MInt {
        val a = MInt(v)
        a += b
        return a
    }

    operator fun minus(b: MInt): MInt {
        val a = MInt(v)
        a -= b
        return a
    }

    operator fun times(b: MInt): MInt {
        val a = MInt(v)
        a *= b
        return a
    }

    operator fun div(b: MInt): MInt {
        val a = MInt(v)
        a /= b
        return a
    }

    operator fun plusAssign(b: MInt) {
        v += b.v
        if (v >= MOD) v -= MOD
    }

    operator fun minusAssign(b: MInt) {
        v -= b.v
        if (v < 0) v += MOD
    }

    operator fun timesAssign(b: MInt) {
        v *= b.v
        v %= MOD
    }

    operator fun divAssign(b: MInt) {
        this *= b.inv()
    }

    fun inv(): MInt {
        var a = v
        var b = MOD
        var u = 1L
        var w = 0L
        while (b != 0L) {
            val t = a / b
            a -= t * b
            u -= t * w
            var tmp = a
            a = b
            b = tmp
            tmp = u
            u = w
            w = tmp
        }
        u %= MOD
        if (u < 0) u += MOD
        return MInt(u)
    }

    fun pow(n: Int): MInt {
        var a = MInt(1)
        for (i in 1..n) a = a * this
        return a
    }

    override fun toString(): String {
        return v.toString()
    }
}