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

import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}
import scala.collection.mutable.{Map, Stack, Queue, PriorityQueue, Set}

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  val MOD = 1e9.toInt + 7

  val N = in.next().toInt
  val A = new Array[Int](N+1)
  for (i <- 1 to N)
    A(i) = in.next().toInt

  //
  // 素数とその個数の組のリストを返す
  def primeFactorization(n: Long): Map[Long, Int] = {
    var rest = n
    val res = Map[Long, Int]()

    var i = 2L
    while (i * i <= n) {
      var cnt = 0
      while (rest % i == 0) {
        cnt += 1
        rest /= i
      }
      if (cnt > 0)
        res += (i -> cnt)

      i += 1
    }
    if (rest > 1)
        res += (rest -> 1)

    res
  }

  def repeatSquares(x: Long, n: Int): Long = {
    def func(x: Long, n: Int): Long = {
      if(n == 0)
        1
      else if(n % 2 == 0)
        func(x*x % MOD, n/2)
      else
        (x * func(x*x % MOD, n/2)) % MOD
    }

    if (x == 0 && n != 0) 0
    else func(x, n)
  }

  val hash = new Array[Map[Long, Int]](N+1)

  val lcm = Map[Long, Int]()
  for (i <- 1 to N) {
    val hoge = primeFactorization(A(i))
    hash(i) = hoge
    for ((p, num) <- hoge) {
      if (!lcm.contains(p))
        lcm += (p -> num)
      else
        lcm(p) = Math.max(lcm(p), num)
    }
  }

  var ans = 0L
  for (i <- 1 to N) {
    val hoge = hash(i)
    var tmp = 1L
    for ((p, num) <- lcm) {
      val n =
        if (!hoge.contains(p)) num
        else {num - hoge(p)}

      tmp *= Math.max(1L, repeatSquares(p, n))
      tmp %= MOD
    }

    ans += tmp
    ans %= MOD
  }

  println(ans)
}

class InputReader(stream: InputStream) {
  private[this] val reader = new BufferedReader(new InputStreamReader(stream))
  private[this] var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}