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 Rust (1.15.1) (judge verdict: Runtime Error)

use std::ops;
use std::fmt;
use std::iter;
use std::collections::HashMap;

macro_rules! get_one {
    ($t:ty) => {{
        let mut line: String = String::new();
        std::io::stdin().read_line(&mut line).unwrap();
        line.trim().parse::<$t>().unwrap()
    }};

    ($t:ty; $n:expr) => {
        (0..$n).map(|_| get_one!($t)).collect::<Vec<_>>()
    };
}

macro_rules! get_vec {
    ($t:ty) => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.split_whitespace()
                .map(|t| t.parse::<$t>().unwrap())
                .collect::<Vec<_>>()
        }
    };
    ($t:ty; $n:expr) => {
        (0..$n).map(|_| get_vec!($t)).collect::<Vec<_>>()
    };
}

fn main() {
    let n = get_one!(i64);
    let v_a = get_vec!(i64);


    // 1000 までの素数を vec にする
    let primes = primes(1000);

    // v_a をそれぞれ素因数分解する
    let v_factored: Vec<_> = v_a.iter().map(|&a| factor(a, &primes)).collect();

    // 各素因数の最大値をとる
    let hm_max = v_factored.into_iter().fold(HashMap::new(), |acc, x| concat_hashmap_max(acc, x));

    // 最大値と，最大値の割り算の和を計算
    let max = {
        let mut m = ModInt::new(1);
        for &k in hm_max.keys() {
            m = m * ModInt::new(k).pow(hm_max[&k] as u32);
        }
        m
    };

    // ModInt で計算！
    let max_div_a_iter = v_a.into_iter().map(|a| max / ModInt::new(a));
    let max_div_a : ModInt = max_div_a_iter.sum();

    println!("{}", max_div_a.0)

}

fn factor(n: i64, primes: &Vec<(i64, i64)>) -> HashMap<i64, i64> {
    let mut n = n;
    let mut hm = HashMap::new();
    if n == 1 {return hm};

    let mut p_idx = 0;
    while n != 1 && primes[p_idx].1 <= n {
        while n % primes[p_idx].0 == 0 {
            n /= primes[p_idx].0;
            let count = hm.entry(primes[p_idx].0).or_insert(0);
            *count += 1;
        }
        p_idx += 1;
    }
    if n != 1 {
        hm.insert(n, 1);
    }
    hm
}

fn concat_hashmap_max(hm1: HashMap<i64, i64>, hm2: HashMap<i64, i64>) -> HashMap<i64, i64> {
    let mut hm = HashMap::new();
    for &k in hm1.keys().chain(hm2.keys()) {
        let max = {
            let a = hm1.get(&k);
            let b = hm2.get(&k);
            match (a, b) {
                (Some(&a), Some(&b)) => {if a > b {a} else {b}},
                (None, Some(&b)) => {b},
                (Some(&a), None) => {a},
                _ => 0
            }
        };
        hm.insert(k, max);
    }
    hm
}

const MOD: i64 = 1_000_000_007;

#[derive(Copy, Clone, Debug, PartialEq)]
struct ModInt (i64);

impl ModInt {
    fn new(value: i64) -> Self {
        ModInt(value % MOD)
    }

    /// a を (MOD - 2) 乗した余りを求める
    /// MOD - 2: 111011100110101100101000000101
    fn resiprocal(&self) -> Self {
        let mut v_res = [ModInt::new(0); 30];

        v_res[0] = self.clone();
        for i in 1..30 {
            v_res[i] = v_res[i - 1] * v_res[i - 1]
        }
        let mut prod = ModInt::new(1);
        for &i in [0, 2, 9, 11, 14, 15, 17, 19, 20, 23, 24, 25, 27, 28, 29].iter() {
            prod = prod * v_res[i]
        }
        prod
    }

    fn pow(&self, num: u32) -> Self {
        let mut res = ModInt::new(1);
        let mut num = num;
        let mut beki = *self;
        while num > 0 {
            if num % 2 == 1 {
                res = res * beki;
            }
            beki = beki * beki;
            num = num >> 1;
        }
        res
    }
}

impl ops::Add for ModInt {
    type Output = Self;
    fn add(self, rhs: ModInt) -> Self::Output {
        ModInt::new(self.0 + rhs.0)
    }
}

impl iter::Sum for ModInt {
    fn sum<I: Iterator<Item = ModInt>>(iter: I) -> Self {
        let mut total = ModInt::new(0);
        for i in iter {
            total = total + i
        }
        total
    }
}

impl ops::Sub for ModInt {
    type Output = Self;
    fn sub(self, rhs: ModInt) -> Self::Output {
        ModInt::new(self.0 - rhs.0)
    }
}

impl ops::Mul for ModInt {
    type Output = Self;
    fn mul(self, rhs: Self) -> Self::Output {
        ModInt::new(self.0 * rhs.0)
    }
}

impl ops::Div for ModInt {
    type Output = Self;
    fn div(self, rhs: Self) -> Self::Output {
        self * rhs.resiprocal()
    }
}

#[cfg(test)]
mod tests {

    use super::ModInt;

    #[test]
    fn resiprocal() {
        let one = ModInt::new(37);
        let res = one.resiprocal();
        let expect = ModInt::new(621621626);

        assert_eq!(expect, res);
    }

    fn division() {
        let a = ModInt::new(11111);
        let b = ModInt::new(271);
        let expect = ModInt::new(41);

        assert_eq!(expect, a / b);
    }

}

fn primes(num: i64) -> Vec<(i64, i64)> {
    let mut primes = vec![];
    let mut cands: Vec<_> = (2..num+1).into_iter().collect();
    cands.reverse();
    while let Some(n) = cands.pop() {
        primes.push(n);
        cands = cands.into_iter().filter(|x| x % n != 0).collect();
    }
    primes.into_iter().map(|n| (n, n * n)).collect()
}

#[test]
fn test_primes() {
    assert_eq!(primes(1000), [(0, 0)])
}