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: Accepted)

import java.io._
import java.util.StringTokenizer

import scala.reflect.ClassTag

object Main {
  def main(args: Array[String]): Unit = {
    try {
      val (in, out) =
        if (isOnlineJudge) {
          (
            new FastScanner(new InputStreamReader(System.in)),
            new PrintWriter(System.out)
          )
        } else {
          (
            new FastScanner(new FileReader(new File("src/resource/problem.in"))),
            new PrintWriter(new File("src/resource/problem.out"))
          )
        }
      new Abc152(in, out).solve()
      out.flush()
      out.close()
    } catch {
      case e: IOException => e.printStackTrace()
    }
  }

  private[this] val isOnlineJudge = true

  class FastScanner(val streamReader: InputStreamReader) {
    private[this] val reader = new BufferedReader(streamReader, 32768)
    private[this] var tokenizer: StringTokenizer = _

    @inline private[this] final def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = Integer.parseInt(next())

    def nextLong(): Long = java.lang.Long.parseLong(next())

    def nextChar(): Char = next().charAt(0)

    def ni(): Int = nextInt()

    def nl(): Long = nextLong()

    def nc(): Char = nextChar()

    def ns(): String = next()

    def ns(n: Int): Array[Char] = ns().toCharArray

    def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)

    def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
      val A1, A2 = Array.ofDim[Int](n)
      REP(n) { i =>
        A1(i) = ni() + offset
        A2(i) = ni() + offset
      }
      (A1, A2)
    }

    def nm(n: Int, m: Int): Array[Array[Int]] = {
      val A = Array.ofDim[Int](n, m)
      REP(n) { i =>
        REP(m) { j =>
          A(i)(j) = ni()
        }
      }
      A
    }

    def nal(n: Int): Array[Long] = map(n)(_ => nl())

    def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n)(_ => ns(m))
  }

  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while (i < N) {
      f(i);
      i += 1
    }
  }

  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while (i >= offset) {
      f(i);
      i -= 1
    }
  }

  def TO(from: Int, to: Int)(f: Int => Unit): Unit = {
    REP(to - from + 1, from)(f)
  }

  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }

  def cumSum(as: Array[Int]): Array[Long] = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

class Abc152(fScanner: Main.FastScanner, out: java.io.PrintWriter) {

  import Main._
  import fScanner._

  def solve(): Unit = {
    val n = ni()
    val a = na(n)
    prePrimes()
//    out.println(primes.length)
//    println(primes.mkString(" "))
    val cnt = Array.fill[Int](N)(0)
    REP(n) { i =>
      var x = a(i)
      var run = true
      var id = 0
      while (run && id < primes.length) {
        if(!mrk(x) && cnt(x) < 1)  {
          cnt(x) = 1
          x = 1
        } else {
          var c = 0
          while (x % primes(id) == 0) {
            c += 1
            x /= primes(id)
          }
          if(c > cnt(primes(id))) cnt(primes(id)) = c
        }
        if(x <= 1) run = false
        id += 1
      }
    }
    var lm = 1L
    REP(N) { i =>
      if(cnt(i) > 0) {
        lm = lm * pow(i, cnt(i))
        lm %= MOD
      }
    }

    var ans = 0L
    REP(n) { i =>
      ans += (lm * modInv(a(i))) % MOD
      ans %= MOD
    }

    out.println(ans)
  }

  val N: Int = (1e6 + 1).toInt
  val MOD: Int = (1e9 + 7).toInt
  var primes: Array[Int] = _
  var mrk: Array[Boolean] = _

  def prePrimes(): Unit = {
    mrk = Array.fill[Boolean](N)(false)
    mrk(0) = true
    mrk(1) = true
    REP(N) { i =>
      if(i >= 2 && !mrk(i)) {
        i*2 until N by i foreach(j => mrk(j) = true)
      }
    }
    primes = mrk.zipWithIndex.filter(!_._1).map(_._2)
  }

  def pow(u: Long, n: Int): Long = {
    var ans = 1L
    var v = n
    var ret = u.toLong
    while (v > 0) {
      if(v % 2 != 0) {
        ans = ans * ret
        ans %= MOD
      }
      ret = ret * ret
      ret %= MOD
      v /= 2
    }
    ans
  }

  def modInv(x: Long): Long = {
    pow(x, MOD-2)
  }
}