Problem:
Score : 500 points

Problem Statement

Consider sequences \{A_1,...,A_N\} of length N consisting of integers between 1 and K (inclusive).

There are K^N such sequences. Find the sum of \gcd(A_1, ..., A_N) over all of them.

Since this sum can be enormous, print the value modulo (10^9+7).

Here \gcd(A_1, ..., A_N) denotes the greatest common divisor of A_1, ..., A_N.

Constraints

2 \leq N \leq 10^5

1 \leq K \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the sum of \gcd(A_1, ..., A_N) over all K^N sequences, modulo (10^9+7).

Sample Input 1

3 2

Sample Output 1

9

\gcd(1,1,1)+\gcd(1,1,2)+\gcd(1,2,1)+\gcd(1,2,2)
+\gcd(2,1,1)+\gcd(2,1,2)+\gcd(2,2,1)+\gcd(2,2,2)
=1+1+1+1+1+1+1+2=9

Thus, the answer is 9.

Sample Input 2

3 200

Sample Output 2

10813692

Sample Input 3

100000 100000

Sample Output 3

742202979

Be sure to print the sum modulo (10^9+7).

Solution in C# (.NET Core 3.1.201) (judge verdict: Runtime Error)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class AtCoderF
{
    private static long[] memo;

    static void Main()
    {
        var sc = new Scanner();

        int n = sc.NextInt();
        long[] a = sc.LongArray();

        memo = new long[n + 1];
        for (int i = 0; i < n + 1; i++)
        {
            memo[i] = long.MinValue;
        }

        if (n % 2 == 0)
        {
            long answer1 = 0;
            for (int i = 0; i < n; i += 2)
            {
                answer1 += a[i];
            }
            long answer2 = 0;
            for (int i = 1; i < n; i += 2)
            {
                answer2 += a[i];
            }
            Console.WriteLine(Math.Max(answer1, answer2));
            return;
        }

        long answer = GetAnswer(a);

        Console.WriteLine(answer);
    }

    private static long GetAnswer(long[] array)
    {
        int length = array.Length;

        if (memo[length] != long.MinValue)
        {
            return memo[length];
        }

        long answer = 0;

        // 偶数の場合
        if (length % 2 == 0)
        {
            long answer1 = 0;
            for (int i = 0; i < length; i += 2)
            {
                answer1 += array[i];
            }
            long answer2 = 0;
            for (int i = 1; i < length; i += 2)
            {
                answer2 += array[i];
            }
            answer = Math.Max(answer1, answer2);
        }
        else
        {
            // 1番目を選んだ場合
            long[] array1 = new long[length - 2];
            Array.Copy(array, 2, array1, 0, length - 2);
            long answer1;
            if (length == 3)
            {
                answer1 = array[0];
            }
            else
            {
                answer1 = array[0] + GetAnswer(array1);
            }

            // 2番目を選んだ場合
            long[] array2 = new long[length - 3];
            Array.Copy(array, 3, array2, 0, length - 3);
            long answer2;
            if (length == 3)
            {
                answer2 = array[1];
            }
            else
            {
                answer2 = array[1] + GetAnswer(array2);
            }

            // どちらも選ばなかった場合
            long answer3 = 0;
            for (int i = 2; i < length; i += 2)
            {
                answer3 += array[i];
            }

            answer = Math.Max(answer1, Math.Max(answer2, answer3));
        }

        memo[length] = answer;

        return answer;
    }

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            while (_index >= _line.Length)
            {
                _line = Console.ReadLine().Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            _line = Console.ReadLine().Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}