Problem p02715 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define each(i, c) for (auto &i : c)
#define mkp(a, b) make_pair(a, b)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> Pll;
const ll MOD = 1e9 + 7;

template <typename P, typename Q>
ostream &operator<<(ostream &os, pair<P, Q> p) {
  os << "(" << p.first << ": " << p.second << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  os << "(";
  each(i, v) os << i << ", ";
  os << ")";
  return os;
}
template <typename K, typename V>
ostream &operator<<(ostream &os, map<K, V> m) {
  os << "{";
  each(i, m) os << i << ", ";
  os << "}";
  return os;
}

class modint {
private:
  ll m_val;
  ll m_mod;

public:
  constexpr modint() : m_val(0), m_mod() { m_mod = MOD; }
  constexpr modint(const ll x) : m_val(0), m_mod(0) {
    m_mod = MOD;
    m_val = x % m_mod;
  }
  constexpr modint(const ll x, const ll mod) : m_val(0), m_mod(0) {
    m_mod = mod;
    m_val = x % m_mod;
  }
  constexpr ll val() const { return m_val; }
  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;
  }
  constexpr modint &operator+=(const modint &rhs) {
    m_val += rhs.m_val;
    if (m_val >= m_mod)
      m_val -= m_mod;
    return *this;
  }
  constexpr modint &operator-=(const modint &rhs) {
    if (m_val < rhs.m_val)
      m_val += m_mod;
    m_val -= rhs.m_val;
    return *this;
  }
  constexpr modint &operator*=(const modint &rhs) {
    m_val = m_val * rhs.m_val % m_mod;
    return *this;
  }
  constexpr modint &operator/=(const modint &rhs) {
    *this *= rhs.pow(m_mod - 2);
    return *this;
  }
  constexpr modint pow(ll n) const {
    if (n == 0)
      return 1;
    modint o = pow(n >> 1);
    o *= o;
    if (n & 1)
      o *= *this;
    return o;
  }
  friend ostream &operator<<(ostream &os, const modint &a) {
    os << a.val();
    return os;
  }
};

ll modpow(ll x, ll n) {
  ll res = 1;
  while (n != 0) {
    if (n & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return res;
}

int main() {
  ll n, k;
  cin >> n >> k;

  vector<ll> memo(k + 1);
  for (ll i = k; i >= 1; --i) {
    ll a = modpow(k / i, n);
    memo[i] = a;
    for (ll j = 2; i * j <= k; ++j) {
      memo[i] -= memo[i * j];
    }
  }

  modint ans(0);
  for (ll i = 1; i <= k; ++i) {
    ans += (i * memo[i]);
  }
  cout << ans.val() << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define each(i, c) for (auto &i : c)
#define mkp(a, b) make_pair(a, b)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> Pll;
const ll MOD = 1e9 + 7;

template <typename P, typename Q>
ostream &operator<<(ostream &os, pair<P, Q> p) {
  os << "(" << p.first << ": " << p.second << ")";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  os << "(";
  each(i, v) os << i << ", ";
  os << ")";
  return os;
}
template <typename K, typename V>
ostream &operator<<(ostream &os, map<K, V> m) {
  os << "{";
  each(i, m) os << i << ", ";
  os << "}";
  return os;
}

class modint {
private:
  ll m_val;
  ll m_mod;

public:
  constexpr modint() : m_val(0), m_mod() { m_mod = MOD; }
  constexpr modint(const ll x) : m_val(0), m_mod(0) {
    m_mod = MOD;
    m_val = x % m_mod;
  }
  constexpr modint(const ll x, const ll mod) : m_val(0), m_mod(0) {
    m_mod = mod;
    m_val = x % m_mod;
  }
  constexpr ll val() const { return m_val; }
  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;
  }
  constexpr modint &operator+=(const modint &rhs) {
    m_val += rhs.m_val;
    if (m_val >= m_mod)
      m_val -= m_mod;
    return *this;
  }
  constexpr modint &operator-=(const modint &rhs) {
    if (m_val < rhs.m_val)
      m_val += m_mod;
    m_val -= rhs.m_val;
    return *this;
  }
  constexpr modint &operator*=(const modint &rhs) {
    m_val = m_val * rhs.m_val % m_mod;
    return *this;
  }
  constexpr modint &operator/=(const modint &rhs) {
    *this *= rhs.pow(m_mod - 2);
    return *this;
  }
  constexpr modint pow(ll n) const {
    if (n == 0)
      return 1;
    modint o = pow(n >> 1);
    o *= o;
    if (n & 1)
      o *= *this;
    return o;
  }
  friend ostream &operator<<(ostream &os, const modint &a) {
    os << a.val();
    return os;
  }
};

ll modpow(ll x, ll n) {
  ll res = 1;
  while (n != 0) {
    if (n & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return res;
}

int main() {
  ll n, k;
  cin >> n >> k;

  vector<ll> memo(n + 1);
  for (ll i = k; i >= 1; --i) {
    ll a = modpow(k / i, n);
    memo[i] = a;
    for (ll j = 2; i * j <= k; ++j) {
      memo[i] -= memo[i * j];
    }
  }

  modint ans(0);
  for (ll i = 1; i <= k; ++i) {
    ans += (i * memo[i]);
  }
  cout << ans.val() << endl;

  return 0;
}