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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Numerics;
using System.Text;

public class Test_152_e
{
  
        /// <summary>
        /// テスト環境の動作確認用
        /// </summary>
        public static void Main()
        {

            var N = Int32.Parse(Console.ReadLine());
            int[] a = new int[N];
            string[] ss = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < N; i++)
            {
                a[i] = int.Parse(ss[i]);
            }

            Console.WriteLine(Solve(a).ToString());
        }
  
        public static long Solve(int[] a)
        {

            var N = a.Length;

            int maxNum = a.Max();

            var prime = GeneratePrimesNaive(maxNum);
            PrimeDigit d = new PrimeDigit(1, prime);
            PrimeDigit[] rr = new PrimeDigit[N];
            for (int i = 0; i < N; i++)
            {
                PrimeDigit r = new PrimeDigit(a[i], prime);
                rr[i] = r;
                d = Gcd(d, r);
            }
            // 最小公倍数が求められた
            ModInt ans = ModInt.Zero;
            for (int i = 0; i < N; i++)
            {
                PrimeDigit t = Divide(d, rr[i]);
                ans += t.ToModInt();
            }

            return ans.Val;
        }

        static PrimeDigit Divide(PrimeDigit a, PrimeDigit b)
        {
            List<int> keys = a.data.Keys.Union(b.data.Keys).Distinct().ToList();
            Dictionary<int, int> map = new Dictionary<int, int>();
            foreach (int key in keys)
            {
                //int n1 = a.data.GetValueOrDefault(key);
                //int n2 = b.data.GetValueOrDefault(key);

                int n1 = 0;
                int n2 = 0;
                a.data.TryGetValue(key, out n1);
                b.data.TryGetValue(key, out n2);

                int n = n1 - n2;
                if (n != 0)
                {
                    map[key] = n;
                }
            }

            return new PrimeDigit(map);
        }

        /// <summary>
        /// 最小公倍数を求める
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        static PrimeDigit Gcd(PrimeDigit a, PrimeDigit b)
        {
            // 最小公倍数 を求める
            List<int> keys = a.data.Keys.Union(b.data.Keys).Distinct().ToList();
            Dictionary<int, int> map = new Dictionary<int, int>();
            foreach (int key in keys)
            {
				int n1 = 0;
                int n2 = 0;
                a.data.TryGetValue(key, out n1);
                b.data.TryGetValue(key, out n2);
              
                map[key] = Math.Max(n1, n2);
            }

            return new PrimeDigit(map);
        }
        /// <summary>
        /// 数字を素数であらわす
        /// </summary>
        public class PrimeDigit
        {
            /// <summary>
            /// 数字を素因数分解して格納する領域
            /// </summary>
            public Dictionary<int, int> data = new Dictionary<int, int>();

            public PrimeDigit(int num, List<int> prime)
            {
                int x = num;
              int sqrtMaxNum = (int)Math.Sqrt(num);
                // 素数に分解
                foreach (int n in prime)
                {
                    int m = 0;
                    while (x % n == 0)
                    {
                        m++;
                        x /= n;
                    }
                    if (m > 0)
                    {
                        data[n] = m;
                    }
                    if (x == 1) break;
                    if (x > sqrtMaxNum)
                    {
                        int d = prime.IndexOf(x);
                        data[d] = 1;
                        break;
                    }
                }
            }

            public PrimeDigit(Dictionary<int, int> map)
            {
                foreach (int n in map.Keys)
                {
                    if (map[n] > 0)
                    {
                        data[n] = map[n];
                    }
                }
            }

            public BigInteger ToBigInteger()
            {
                BigInteger sum = BigInteger.One;
                foreach (int n in data.Keys)
                {
                    for (int i = 0; i < data[n]; i++)
                    {
                        sum *= n;
                    }
                }
                return sum;
            }

            public override string ToString()
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("{" + this.ToBigInteger().ToString() + ":");
                foreach (int key in data.Keys)
                {
                    sb.Append("<" + key + "^" + data[key].ToString() + ">");
                }
                sb.Append("}");

                return sb.ToString();
            }

            internal ModInt ToModInt()
            {
                ModInt sum = ModInt.One;
                foreach (int n in data.Keys)
                {
                    for (int i = 0; i < data[n]; i++)
                    {
                        sum *= ModInt.ValueOf(n);
                    }
                }
                return sum;
            }
        }

        /// <summary>
        /// 指定の数以下の 素数を一覧で表示する
        /// </summary>
        /// <param name="MaxNum"></param>
        /// <returns></returns>
        public static List<int> GeneratePrimesNaive(int MaxNum)
        {
            List<int> primes = new List<int>();
            primes.Add(2);
            int nextPrime = 3;
            while (nextPrime <= MaxNum)
            {
                int sqrt = (int)Math.Sqrt(nextPrime);
                bool isPrime = true;
                for (int i = 0; (int)primes[i] <= sqrt; i++)
                {
                    if (nextPrime % primes[i] == 0)
                    {
                        isPrime = false;
                        break;
                    }
                }
                if (isPrime)
                {
                    primes.Add(nextPrime);
                }
                nextPrime += 2;
            }
            return primes;
        }
  
  	public class ModInt
    {
        /// <summary>
        /// 基準となる素数
        /// </summary>
        const long BASE_NUM = 1000000007;

        public long Val { get; private set; }

        long n = 0;

        public static ModInt Zero { get; private set; } = new ModInt(0);

        public static ModInt One { get; private set; } = new ModInt(1);

        public ModInt(long n)
        {
            Val = n % BASE_NUM;
        }


        public static ModInt operator +(ModInt a, ModInt b)
        {
            if (a.Val == 0) return b;
            if (b.Val == 0) return a;

            return new ModInt(a.Val + b.Val);
        }

        public static ModInt operator -(ModInt a, ModInt b)
        {
            if (a.Val == 0) return b;
            if (b.Val == 0) return a;

            return new ModInt(a.Val - b.Val);
        }

        public static ModInt operator /(ModInt a, ModInt b)
        {
            return new ModInt(a.Val / b.Val);
        }

        public static ModInt operator *(ModInt a, ModInt b)
        {
            if (a.Val == 0) return b;
            if (b.Val == 0) return a;

            return new ModInt(a.Val * b.Val);
        }

        public static ModInt ValueOf(int n)
        {
            return new ModInt(n);
        }
    }
}