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)

#![allow(dead_code)]
use crate::misc::CONST_1E9_7;
use crate::modulo::*;
use whiteread::Reader;
modint!(CONST_1E9_7);
fn main() {
    let mut scan = Reader::from_stdin_naive();
    let n = scan.p::<i64>();
    let k = scan.p::<usize>();
    let mut cnt = vec![Z::new(0); k + 1];
    for g in (1..=k).rev() {
        cnt[g] = Z::new(k / g).pow(n);
        let mut ng = g * 2;
        while ng <= k as usize {
            let tmp = cnt[ng];
            cnt[g] -= tmp;
            ng += g;
        }
    }
    let mut ret = Z::new(0);
    for g in 1..=k {
        ret += cnt[g] * g.into();
    }
    println!("{}", ret);
}
mod misc {
    use crate::tiny_num_traits::ZeroOne;
    use std::collections::{BTreeSet, HashMap};
    use std::ops::{Add, Div, Mul, Sub};
    pub const CONST_1E9_7: i64 = 1_000_000_007;
    pub fn arr<'a, S, T>(n: usize, mut f: S) -> Vec<T>
    where
        S: FnMut(usize) -> T + 'a,
    {
        (0..n).map(|i| f(i)).collect::<Vec<T>>()
    }
    pub fn adjacent4(
        y: usize,
        x: usize,
        h: usize,
        w: usize,
    ) -> impl Iterator<Item = (usize, usize)> {
        const DYDX: [(isize, isize); 4] = [(-1, 0), (1, 0), (0, -1), (0, 1)];
        DYDX.iter().filter_map(move |&(dy, dx)| {
            let ny = y as isize + dy;
            let nx = x as isize + dx;
            if nx >= 0 && nx < w as isize && ny >= 0 && ny < h as isize {
                Some((ny as usize, nx as usize))
            } else {
                None
            }
        })
    }
    pub fn adjacent8(
        y: usize,
        x: usize,
        h: usize,
        w: usize,
    ) -> impl Iterator<Item = (usize, usize)> {
        const DYDX: [(isize, isize); 8] = [
            (-1, 0),
            (1, 0),
            (0, -1),
            (0, 1),
            (-1, -1),
            (-1, 1),
            (1, -1),
            (1, 1),
        ];
        DYDX.iter().filter_map(move |&(dy, dx)| {
            let ny = y as isize + dy;
            let nx = x as isize + dx;
            if nx >= 0 && nx < w as isize && ny >= 0 && ny < h as isize {
                Some((ny as usize, nx as usize))
            } else {
                None
            }
        })
    }
    pub fn run_length_encoding<T: Clone + PartialEq>(slice: &[T]) -> Vec<(i64, T)> {
        if slice.len() == 0 {
            return vec![];
        }
        let mut acc = vec![];
        let mut current = slice[0].clone();
        let mut cnt = 1i64;
        for i in 1..slice.len() {
            if current != slice[i] {
                acc.push((cnt, current));
                cnt = 0;
            }
            current = slice[i].clone();
            cnt += 1;
        }
        acc.push((cnt, current));
        acc
    }
    pub fn count<T: Eq + std::hash::Hash + Clone>(
        slice: &[T],
    ) -> std::collections::HashMap<T, usize> {
        let mut hmap = std::collections::HashMap::new();
        for item in slice {
            *hmap.entry(item.clone()).or_insert(0) += 1;
        }
        hmap
    }
    pub fn indices_by_elem<T: Eq + std::hash::Hash + Clone>(
        slice: &[T],
    ) -> std::collections::HashMap<T, Vec<usize>> {
        let mut hmap = std::collections::HashMap::new();
        for i in 0..slice.len() {
            hmap.entry(slice[i].clone()).or_insert(vec![]).push(i);
        }
        hmap
    }
    pub fn combine<S: Clone, T: Clone, U: std::iter::FromIterator<(S, T)>>(
        left: &[S],
        right: &[T],
    ) -> U {
        let mut ret = vec![];
        for i in 0..left.len() {
            for j in 0..right.len() {
                ret.push((left[i].clone(), right[j].clone()));
            }
        }
        ret.into_iter().collect::<U>()
    }
    pub fn uncount<T: Clone + Eq + std::hash::Hash>(
        hmap: &std::collections::HashMap<T, usize>,
    ) -> Vec<T> {
        hmap.iter()
            .map(|(k, v)| vec![k.clone(); *v])
            .flatten()
            .collect()
    }
    pub fn max_with_index<T: Ord>(slice: &[T]) -> (usize, &T) {
        let mut value: Option<&T> = None;
        let mut index = 0;
        for i in 0..slice.len() {
            if let Some(v) = value {
                if v < &slice[i] {
                    index = i;
                    value = Some(&slice[i]);
                }
            } else {
                index = i;
                value = Some(&slice[i]);
            }
        }
        (index, value.expect("not found max value"))
    }
    pub fn min_with_index<T: Ord>(slice: &[T]) -> (usize, &T) {
        let mut value: Option<&T> = None;
        let mut index = 0;
        for i in 0..slice.len() {
            if let Some(v) = value {
                if v > &slice[i] {
                    index = i;
                    value = Some(&slice[i]);
                }
            } else {
                index = i;
                value = Some(&slice[i]);
            }
        }
        (index, value.expect("not found min value"))
    }
    pub fn split<T: PartialEq + Clone>(slice: &[T], sep: T) -> Vec<Vec<T>> {
        slice.iter().fold(vec![vec![]], |mut acc, x| {
            if x == &sep {
                acc.push(vec![]);
                acc
            } else {
                let last = acc.len() - 1;
                acc[last].push(x.clone());
                acc
            }
        })
    }
    pub fn join<T: std::fmt::Display>(slice: &[T], sep: &str) -> String {
        let strings = slice.iter().map(|t| format!("{}", t)).collect::<Vec<_>>();
        strings.join(sep)
    }
    pub fn coord_comp(slice: &[i64]) -> (Vec<i64>, HashMap<i64, usize>) {
        let mut set = BTreeSet::new();
        for &item in slice {
            set.insert(item);
        }
        let mut hmap = HashMap::new();
        for (i, &v) in set.iter().enumerate() {
            hmap.insert(v, i);
        }
        (set.into_iter().collect::<Vec<_>>(), hmap)
    }
    pub fn shakutori(n: usize, k: i64, a: &[i64]) -> usize {
        let mut sum = 0;
        let mut right = 0;
        let mut ret = 0;
        for left in 0..n {
            while right < n && sum <= k {
                sum += a[right];
                right += 1;
            }
            ret += right - left;
            if right == left {
                right += 1;
            } else {
                sum -= a[left];
            }
        }
        ret
    }
    pub fn inverse(a: &[usize], n: usize) -> Vec<usize> {
        let mut inv = vec![0; n];
        for i in 0..a.len() {
            inv[a[i]] = i;
        }
        inv
    }
    pub fn pow(x: i64, y: i64) -> i64 {
        (x as f64).powi(y as i32) as i64
    }
    pub fn ceil_div<T: Copy + ZeroOne + Add<Output = T> + Sub<Output = T> + Div<Output = T>>(
        x: T,
        y: T,
    ) -> T {
        (x + y - T::one()) / y
    }
    pub fn ceil_mod<
        T: Copy + ZeroOne + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>,
    >(
        x: T,
        y: T,
    ) -> T {
        ceil_div(x, y) * y
    }
    pub fn unzip<P: Clone, Q: Clone>(v: &[(P, Q)]) -> (Vec<P>, Vec<Q>) {
        (
            v.iter().map(|t| t.0.clone()).collect(),
            v.iter().map(|t| t.1.clone()).collect(),
        )
    }
    pub fn unzip3<P: Clone, Q: Clone, R: Clone>(v: &[(P, Q, R)]) -> (Vec<P>, Vec<Q>, Vec<R>) {
        (
            v.iter().map(|t| t.0.clone()).collect(),
            v.iter().map(|t| t.1.clone()).collect(),
            v.iter().map(|t| t.2.clone()).collect(),
        )
    }
    pub fn base(c: char, b: char) -> usize {
        c as usize - b as usize
    }
    pub fn base_signed(c: char, b: char) -> i64 {
        c as i64 - b as i64
    }
    pub fn is_palindrome<T: Clone + Eq>(chars: &[T]) -> bool {
        let s = chars.to_vec();
        let mut t = s.clone();
        t.reverse();
        (0..s.len()).filter(|&i| s[i] == t[i]).count() == s.len()
    }
}
mod tiny_num_traits {
    use crate::modulo::{ConstValue, ModInt};
    pub trait ZeroOne {
        fn zero() -> Self;
        fn one() -> Self;
    }
    impl<T: ConstValue> ZeroOne for ModInt<T> {
        fn zero() -> Self {
            Self::new(0)
        }
        fn one() -> Self {
            Self::new(1)
        }
    }
    macro_rules! impl_zero_one {
        ( $ tpe : ident ) => {
            impl ZeroOne for $tpe {
                fn zero() -> Self {
                    0
                }
                fn one() -> Self {
                    1
                }
            }
        };
    }
    impl_zero_one!(i64);
    impl_zero_one!(i32);
    impl_zero_one!(usize);
}
mod modulo {
    use std::marker::PhantomData;
    use std::ops::*;
    #[derive(Debug, PartialEq, Copy, Clone)]
    pub struct ModInt<T>(pub i64, PhantomData<*const T>);
    impl<T: ConstValue> ModInt<T> {
        pub fn new<U: ModIntCastable>(a: U) -> ModInt<T> {
            ModInt((a.to_long() % T::M + T::M) % T::M, PhantomData)
        }
        pub fn pow(self, mut n: i64) -> Self {
            let mut a = self;
            let mut res = Self::new(1);
            while n > 0 {
                if n & 1 == 1 {
                    res *= a;
                }
                a = a * a;
                n >>= 1;
            }
            res
        }
        pub fn inv(self) -> Self {
            self.pow(T::M - 2)
        }
    }
    pub trait ConstValue: Clone + Copy {
        const M: i64;
    }
    impl<T> std::fmt::Display for ModInt<T> {
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
            write!(f, "{}", self.0)
        }
    }
    impl<T: ConstValue, U: ModIntCastable> From<U> for ModInt<T> {
        fn from(n: U) -> Self {
            Self::new(n)
        }
    }
    impl<T: ConstValue> Add for ModInt<T> {
        type Output = ModInt<T>;
        fn add(self, other: ModInt<T>) -> ModInt<T> {
            Self::new(self.0 + other.0)
        }
    }
    impl<T: ConstValue> AddAssign for ModInt<T> {
        fn add_assign(&mut self, other: Self) {
            *self = self.add(other);
        }
    }
    impl<T: ConstValue> Sub for ModInt<T> {
        type Output = ModInt<T>;
        fn sub(self, other: ModInt<T>) -> ModInt<T> {
            Self::new(self.0 - other.0)
        }
    }
    impl<T: ConstValue> SubAssign for ModInt<T> {
        fn sub_assign(&mut self, other: Self) {
            *self = self.sub(other);
        }
    }
    impl<T: ConstValue> Mul for ModInt<T> {
        type Output = ModInt<T>;
        fn mul(self, other: ModInt<T>) -> ModInt<T> {
            Self::new(self.0 * other.0)
        }
    }
    impl<T: ConstValue> MulAssign for ModInt<T> {
        fn mul_assign(&mut self, other: Self) {
            *self = self.mul(other);
        }
    }
    impl<T: ConstValue> Div for ModInt<T> {
        type Output = ModInt<T>;
        fn div(self, other: ModInt<T>) -> ModInt<T> {
            self * other.inv()
        }
    }
    impl<T: ConstValue> DivAssign for ModInt<T> {
        fn div_assign(&mut self, other: Self) {
            *self = self.div(other);
        }
    }
    #[macro_export]
    macro_rules! modint {
        ( $ m : expr ) => {
            #[derive(Debug, PartialEq, Copy, Clone)]
            pub enum __M {}
            impl ConstValue for __M {
                const M: i64 = $m;
            }
            #[allow(dead_code)]
            type Z = ModInt<__M>;
        };
    }
    pub trait ModIntCastable {
        fn to_long(self) -> i64;
    }
    impl ModIntCastable for i64 {
        fn to_long(self) -> i64 {
            self
        }
    }
    impl ModIntCastable for usize {
        fn to_long(self) -> i64 {
            self as i64
        }
    }
    impl ModIntCastable for i32 {
        fn to_long(self) -> i64 {
            self as i64
        }
    }
}