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)

use proconio::input;
use std::ops::Add;

fn main() {
    input! {
        n: usize,
        k: usize,
    }

    let mut counts = vec![Zp::new(0); k + 1];

    for x in (1..=k).rev() {
        counts[x] = num::pow(Zp::new(k / x), n)
            - (2..)
                .take_while(|m| m * x <= k)
                .map(|m| counts[m * x])
                .fold(Zp::new(0), Add::add);
    }

    let ans = counts
        .into_iter()
        .enumerate()
        .map(|(m, c)| Zp::new(m) * c)
        .fold(Zp::new(0), |a, b| a + b);
    println!("{}", ans);
}

decl_znz!(Zp = Znz<usize, 1_000_000_007>);

pub mod znz {
    use std::{
        borrow::Borrow,
        fmt::{self, Debug, Display},
        marker::PhantomData,
        ops::{Add, BitAnd, Deref, Div, Mul, Rem, Sub},
    };

    #[macro_export]
    macro_rules! decl_znz(($name:ident = Znz<$ty:ty, $modulus:literal>) => {
        type $name = $crate::znz::Znz<$ty, __ConstModulus>;

        #[derive(Debug, Clone, Copy)]
        enum __ConstModulus {}

        impl $crate::znz::Modulus for __ConstModulus {
            type Value = $ty;

            #[inline]
            fn value() -> $ty {
                $modulus
            }
        }
    });

    pub trait Modulus {
        type Value;
        fn value() -> Self::Value;
    }

    pub struct Znz<R, M> {
        repr: R,
        phantom: PhantomData<fn() -> M>,
    }

    use num::One;

    impl<R: PrimitiveInteger + One, M: Modulus<Value = R>> One for Znz<R, M> {
        fn one() -> Self {
            Self::unchecked(R::one())
        }
    }

    impl<R: PrimitiveInteger, M: Modulus<Value = R>> Znz<R, M> {
        #[inline]
        pub fn new(val: R) -> Self {
            Self {
                repr: val % M::value(),
                phantom: PhantomData,
            }
        }

        #[inline]
        fn unchecked(repr: R) -> Self {
            Self {
                repr,
                phantom: PhantomData,
            }
        }

        #[inline]
        pub fn checked_div(self, rhs: Self) -> Option<Self> {
            mod_checked_div(*self, *rhs, M::value()).map(Self::unchecked)
        }

        #[inline]
        pub fn pow<E: PrimitiveInteger>(self, exp: E) -> Self {
            Self::unchecked(mod_pow(*self, exp, M::value()))
        }
    }

    impl<R, M> Deref for Znz<R, M> {
        type Target = R;

        #[inline]
        fn deref(&self) -> &R {
            &self.repr
        }
    }

    impl<R: Copy, M> Clone for Znz<R, M> {
        #[inline]
        fn clone(&self) -> Self {
            Self {
                repr: self.repr,
                phantom: PhantomData,
            }
        }
    }

    impl<R: Copy, M> Copy for Znz<R, M> {}

    impl<R: Display, M> Display for Znz<R, M> {
        #[inline]
        fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
            Display::fmt(&**self, fmt)
        }
    }

    impl<R: Debug, M> Debug for Znz<R, M> {
        #[inline]
        fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
            Debug::fmt(&**self, fmt)
        }
    }

    macro_rules! impl_bin_ops {
    () => {};
    (($($args:tt)*), $($rest:tt)*) => {
        impl_bin_ops!(($($args)*));
        impl_bin_ops!($($rest)*);
    };
    ((<$trait:ident>, $trait_method:ident, $impl_method:ident)) => {
        impl_bin_ops!(@inner(<$trait, Znz<R, M>, Znz<R, M>>, $trait_method, $impl_method));
        impl_bin_ops!(@inner(<$trait, Znz<R, M>, &'_ Znz<R, M>>, $trait_method, $impl_method));
        impl_bin_ops!(@inner(<$trait, &'_ Znz<R, M>, Znz<R, M>>, $trait_method, $impl_method));
        impl_bin_ops!(@inner(<$trait, &'_ Znz<R, M>, &'_ Znz<R, M>>, $trait_method, $impl_method));
    };
    (@inner(<$trait:ident, $lhs_ty:ty, $rhs_ty:ty>, $trait_method:ident, $impl_method:ident)) => {
        impl<R: PrimitiveInteger, M: Modulus<Value = R>> $trait<$rhs_ty> for $lhs_ty {
            type Output = Znz<R, M>;

            #[inline]
            fn $trait_method(self, rhs: $rhs_ty) -> Znz<R, M> {
                Znz::unchecked($impl_method(
                    **Borrow::<Znz<_, _>>::borrow(&self),
                    **Borrow::<Znz<_, _>>::borrow(&rhs),
                    M::value(),
                ))
            }
        }
    };
}

    impl_bin_ops! {
        (<Add>, add, mod_add),
        (<Sub>, sub, mod_sub),
        (<Mul>, mul, mod_mul),
    }

    #[inline]
    pub fn mod_add<T: PrimitiveInteger>(lhs: T, rhs: T, modulus: T) -> T {
        (rhs + lhs) % modulus
    }

    #[inline]
    pub fn mod_sub<T: PrimitiveInteger>(lhs: T, rhs: T, modulus: T) -> T {
        if lhs < rhs {
            modulus + lhs - rhs
        } else {
            lhs - rhs
        }
    }

    #[inline]
    pub fn mod_mul<T: PrimitiveInteger>(lhs: T, rhs: T, modulus: T) -> T {
        (rhs * lhs) % modulus
    }

    #[inline]
    pub fn mod_div<T: PrimitiveInteger>(lhs: T, rhs: T, modulus: T) -> T {
        mod_checked_div(lhs, rhs, modulus).expect("could not divide")
    }

    #[inline]
    pub fn mod_checked_div<T: PrimitiveInteger>(lhs: T, rhs: T, modulus: T) -> Option<T> {
        todo!()
    }

    #[inline]
    pub fn mod_pow<T: PrimitiveInteger, E: PrimitiveInteger>(
        mut base: T,
        mut exp: E,
        modulus: T,
    ) -> T {
        let mut acc = T::ONE;

        let exp_is_neg = exp < E::ZERO;
        if exp_is_neg {
            exp = (E::ZERO - E::ONE) * exp;
        }

        while exp > E::ZERO {
            if (exp & E::ONE) == E::ONE {
                acc = mod_mul(acc, base, modulus);
            }
            exp = exp / (E::ONE + E::ONE);
            base = mod_mul(base, base, modulus);
        }

        if exp_is_neg {
            acc = mod_div(T::ONE, acc, modulus);
        }

        acc
    }

    pub trait PrimitiveInteger:
        Copy
        + Ord
        + Add<Output = Self>
        + Sub<Output = Self>
        + Mul<Output = Self>
        + Div<Output = Self>
        + Rem<Output = Self>
        + BitAnd<Output = Self>
    {
        const ZERO: Self;
        const ONE: Self;
    }

    macro_rules! impl_primitive_integer(($($ty:ty),*) => {
        $(
            impl PrimitiveInteger for $ty {
                const ZERO: Self = 0;
                const ONE: Self = 1;
            }
        )*
    });

    impl_primitive_integer!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize);
}