Problem p02715 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define pii pair<int, int>
#define pli pair<ll, int>
#define pil pair<int, ll>
#define pll pair<ll, ll>
const int inf = 1 << 30;
const ll linf = 1e18;
const ll mod = 1e9 + 7;
template <class T> void chmin(T &x, T y) {
  if (x > y)
    x = y;
}
template <class T> void chmax(T &x, T y) {
  if (x < y)
    x = y;
}

template <long long mod> struct modint {
  long long num;

  constexpr modint(long long x = 0) : num((x + mod) % mod) {}

  constexpr modint &operator+=(const modint &rhs) {
    num = (num + rhs.num) % mod;
    return *this;
  }
  constexpr modint &operator-=(const modint &rhs) {
    num -= rhs.num;
    while (num < 0)
      num += mod;
    num %= mod;
    return *this;
  }
  constexpr modint &operator*=(const modint &rhs) {
    num = num * rhs.num % mod;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) {
    int exp = mod - 2;
    while (exp > 0) {
      if (exp % 2)
        *this *= rhs;
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }

  constexpr modint operator++() {
    ++num;
    return *this;
  }

  constexpr modint operator++(int n) {
    modint tmp = *this;
    ++(*this);
    return tmp;
  }

  constexpr modint operator--() {
    --num;
    return *this;
  }
  constexpr modint operator--(int n) {
    const modint tmp = *this;
    --(*this);
    return tmp;
  }

  constexpr modint operator+(const modint &rhs) const {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint &rhs) const {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint &rhs) const {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint &rhs) const {
    return modint(*this) /= rhs;
  }

  modint modpow(long long exp) {
    modint res = 1;
    while (exp > 0) {
      if (exp % 2)
        res *= (*this);
      (*this) *= (*this);
      exp /= 2;
    }
    (*this) = res;
  }

  friend ostream &operator<<(ostream &lhs, const modint &rhs) {
    return lhs << rhs.num;
  }

  friend istream &operator>>(istream &lhs, modint &rhs) {
    long long tmp;
    lhs >> rhs.num;
    return lhs;
  }
};

#define mint modint<1000000007>

int N, K;
mint sum = 0;
mint cnt[100010];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N >> K;
  for (int i = 1; i <= K; i++) {
    cnt[i] = K / i;
    cnt[i].modpow(N);
  }
  for (int i = K; i >= 1; i--) {
    for (int j = i + i; j <= K; j += i) {
      cnt[i] -= cnt[j];
    }
    sum += cnt[i] * i;
  }
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define pii pair<int, int>
#define pli pair<ll, int>
#define pil pair<int, ll>
#define pll pair<ll, ll>
const int inf = 1 << 30;
const ll linf = 1e18;
const ll mod = 1e9 + 7;
template <class T> void chmin(T &x, T y) {
  if (x > y)
    x = y;
}
template <class T> void chmax(T &x, T y) {
  if (x < y)
    x = y;
}

template <long long mod> struct modint {
  long long num;

  constexpr modint(long long x = 0) : num((x + mod) % mod) {}

  constexpr modint &operator+=(const modint &rhs) {
    num = (num + rhs.num) % mod;
    return *this;
  }
  constexpr modint &operator-=(const modint &rhs) {
    num -= rhs.num;
    while (num < 0)
      num += mod;
    num %= mod;
    return *this;
  }
  constexpr modint &operator*=(const modint &rhs) {
    num = num * rhs.num % mod;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) {
    int exp = mod - 2;
    while (exp > 0) {
      if (exp % 2)
        *this *= rhs;
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }

  constexpr modint operator++() {
    ++num;
    return *this;
  }

  constexpr modint operator++(int n) {
    modint tmp = *this;
    ++(*this);
    return tmp;
  }

  constexpr modint operator--() {
    --num;
    return *this;
  }
  constexpr modint operator--(int n) {
    const modint tmp = *this;
    --(*this);
    return tmp;
  }

  constexpr modint operator+(const modint &rhs) const {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint &rhs) const {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint &rhs) const {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint &rhs) const {
    return modint(*this) /= rhs;
  }

  void modpow(long long exp) {
    modint res = 1;
    while (exp > 0) {
      if (exp % 2)
        res *= (*this);
      (*this) *= (*this);
      exp /= 2;
    }
    (*this) = res;
  }

  friend ostream &operator<<(ostream &lhs, const modint &rhs) {
    return lhs << rhs.num;
  }

  friend istream &operator>>(istream &lhs, modint &rhs) {
    long long tmp;
    lhs >> rhs.num;
    return lhs;
  }
};

#define mint modint<1000000007>

int N, K;
mint sum = 0;
mint cnt[100010];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N >> K;
  for (int i = 1; i <= K; i++) {
    cnt[i] = K / i;
    cnt[i].modpow(N);
  }
  for (int i = K; i >= 1; i--) {
    for (int j = i + i; j <= K; j += i) {
      cnt[i] -= cnt[j];
    }
    sum += cnt[i] * i;
  }
  cout << sum << endl;
  return 0;
}