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 Rust (1.42.0) (judge verdict: Accepted)


mod fp {
    use std::ops::{Add, AddAssign, Sub, SubAssign, Mul, MulAssign, Div, DivAssign};
    use num::One;
    pub trait ConstUSize {
        const VALUE: usize;
    }

    #[derive(Clone, Copy, PartialEq, Eq, Debug)]
    pub struct C10E9_7;
    impl ConstUSize for C10E9_7 {
        const VALUE: usize = 1000000007;
    }

    #[derive(Clone, Copy, PartialEq, Eq, Debug)]
    pub struct Fp<C> {
        v: usize,
        phantom: std::marker::PhantomData<C>,
    }

    impl<C:ConstUSize> Fp<C> {
        fn p() -> usize {
            C::VALUE
        }

        pub fn value(&self) -> usize {
            self.v
        }

        pub fn new(v: usize) -> Self{
            Self {
                v: v % Self::p(),
                phantom: std::marker::PhantomData,
            }
        }
    }

    impl<C:ConstUSize> From<usize> for Fp<C> {
        fn from(v: usize) -> Self {
            Self::new(v)
        }   
    }

    impl<C:ConstUSize> Into<usize> for Fp<C> {
        fn into(self) -> usize {
            self.v
        }
    }

    impl<C:ConstUSize> Add for Fp<C> {
        type Output = Self;
        fn add(self, other: Self) -> Self {
            (self.v as usize + other.v).into()
        }
    }

    impl<C:ConstUSize> AddAssign for Fp<C> where Self: Clone {
        fn add_assign(&mut self, other: Self) {
            *self = self.clone() + other;
        }
    }

    impl<C:ConstUSize> Sub for Fp<C> {
        type Output = Self;
        fn sub(self, other: Self) -> Self {
            (self.v as usize + Self::p() - other.v).into()
        }
    }

    impl<C:ConstUSize> SubAssign for Fp<C> where Self: Clone {
        fn sub_assign(&mut self, other: Self) {
            *self = self.clone() - other;
        }
    }

    impl<C:ConstUSize> Mul for Fp<C> {
        type Output = Self;
        fn mul(self, other: Self) -> Self {
            (self.v as usize * other.v).into()
        }
    }

    impl<C:ConstUSize> MulAssign for Fp<C> where Self: Clone {
        fn mul_assign(&mut self, other: Self) {
            *self = self.clone() * other;
        }
    }

    impl<C:ConstUSize> One for Fp<C> {
        fn one() -> Self {
            Self::new(1)
        }
    }

    impl<C:ConstUSize> Div for Fp<C> where Self: Clone {
        type Output = Self;
        fn div(self, other: Self) -> Self {
            let inv = num::pow::pow(other, Self::p() - 2);
            self * inv
        }
    }

    impl<C:ConstUSize> DivAssign for Fp<C> where Self: Clone {
        fn div_assign(&mut self, other: Self) {
            *self = self.clone() / other;
        }
    }
}

type F = fp::Fp<fp::C10E9_7>;
fn main() {
    use num::pow::pow;
    proconio::input! {
        n: usize,
        k: usize,
    }

    let mut counts = vec![F::new(0); k + 1];
    for i in (1..=k).rev() {
        counts[i] = pow(F::new(k / i), n);
        for j in 1..=k {
            if i * (j + 1) <= k {
                let s = counts[i * (j+1)];
                counts[i] -= s;
            } else {
                break;
            }
        }
    }

    let sum: F = counts.iter().enumerate().map(|(i, c)| F::new(i) * *c)
        .fold(0.into(), std::ops::Add::add);

    println!("{}", sum.value());
}