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

#include <bits/stdc++.h>
// #include <boost/array.hpp>
// #include <boost/range/numeric.hpp>
// #include <boost/range/algorithm.hpp>
// #include <boost/multiprecision/cpp_int.hpp>

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
// using namespace boost::multiprecision;

constexpr long long INF = LLONG_MAX;
constexpr long long MOD = 1000000007;
constexpr long double PI = M_PI;

#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); ++i)
#define RFOR(i, n, r) for (ll i = (ll)(n - 1); i >= r; --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, n, 0)
#define ALL(x) x.begin(), x.end()
#define CALL(x) x.cbegin(), x.cend()
#define RALL(x) x.rbegin(), x.rend()
#define CRALL(x) x.crbegin(), x.crend()

#define cout cout << fixed << setprecision(12)
#define dbglf cerr << '\\n'

#define ACCM(arr) accumulate(ALL(arr), 0LL)
#define Yes(ex) cout << ((ex) ? "Yes" : "No") << endl
#define YES(ex) cout << ((ex) ? "YES" : "NO") << endl

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;
using vp = vector<pair<ll, ll>>;
using vs = vector<string>;
using vc = vector<char>;
using lst = list<ll>;
using pq = priority_queue<ll>;

template <typename T> inline void veccin(vector<T> &v) {
  for (auto &&e : v) {
    cin >> e;
  }
}

template <typename T> inline void dbg(T a) { cerr << a << ' '; }

ll n, m, k;
// ld d;
string s /*1, s2, s3*/;
char c /*1, c2, c3*/;
bool flag = false;

/*--------------------template--------------------*/

// Mod-Int
template <int mod> struct ModInt {
  int x;
  ModInt() : x(0) {}
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }
  static int get_mod() { return mod; }
};
using modint = ModInt<MOD>;

// GCD
ll gcd(ll a, ll b) {
  if (a == b || a == 0)
    return b;
  else if (a < b)
    return gcd(b % a, a);
  else
    return gcd(b, a);
}

// Mod-Pow
template <typename T> T modpow(T x, T n, T mod) {
  if (n == 0)
    return 1;
  T resp = modpow(x * x % mod, n / 2, mod);
  if (n & 1)
    resp = resp * x % mod;
  return resp;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n >> k;
  modint ans(0);
  vi prime(n, 1);
  prime[0] = prime[1] = 0;
  REP(i, n) {
    if (prime[i]) {
      dbg(i);
      for (ll j = i + i; j < n; j += i) {
        prime[j] = 0;
      }
    }
  }

  vi nl(k);
  RREP(i, k) {
    nl[i] = modpow(k / (i + 1), n, MOD);
    for (ll j = 2; j * (i + 1) <= k; ++j) {
      nl[i] -= nl[j * (i + 1) - 1];
    }
    ans += (i + 1) * nl[i];
    dbg(nl[i]);
  }
  cout << ans << endl;
}


Error: 0
2 

Fixed submission:

#include <bits/stdc++.h>
// #include <boost/array.hpp>
// #include <boost/range/numeric.hpp>
// #include <boost/range/algorithm.hpp>
// #include <boost/multiprecision/cpp_int.hpp>

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
// using namespace boost::multiprecision;

constexpr long long INF = LLONG_MAX;
constexpr long long MOD = 1000000007;
constexpr long double PI = M_PI;

#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); ++i)
#define RFOR(i, n, r) for (ll i = (ll)(n - 1); i >= r; --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, n, 0)
#define ALL(x) x.begin(), x.end()
#define CALL(x) x.cbegin(), x.cend()
#define RALL(x) x.rbegin(), x.rend()
#define CRALL(x) x.crbegin(), x.crend()

#define cout cout << fixed << setprecision(12)
#define dbglf cerr << '\\n'

#define ACCM(arr) accumulate(ALL(arr), 0LL)
#define Yes(ex) cout << ((ex) ? "Yes" : "No") << endl
#define YES(ex) cout << ((ex) ? "YES" : "NO") << endl

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;
using vp = vector<pair<ll, ll>>;
using vs = vector<string>;
using vc = vector<char>;
using lst = list<ll>;
using pq = priority_queue<ll>;

template <typename T> inline void veccin(vector<T> &v) {
  for (auto &&e : v) {
    cin >> e;
  }
}

template <typename T> inline void dbg(T a) { cerr << a << ' '; }

ll n, m, k;
// ld d;
string s /*1, s2, s3*/;
char c /*1, c2, c3*/;
bool flag = false;

/*--------------------template--------------------*/

// Mod-Int
template <int mod> struct ModInt {
  int x;
  ModInt() : x(0) {}
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }
  static int get_mod() { return mod; }
};
using modint = ModInt<MOD>;

// GCD
ll gcd(ll a, ll b) {
  if (a == b || a == 0)
    return b;
  else if (a < b)
    return gcd(b % a, a);
  else
    return gcd(b, a);
}

// Mod-Pow
template <typename T> T modpow(T x, T n, T mod) {
  if (n == 0)
    return 1;
  T resp = modpow(x * x % mod, n / 2, mod);
  if (n & 1)
    resp = resp * x % mod;
  return resp;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> n >> k;
  modint ans(0);
  vi prime(n, 1);
  prime[0] = prime[1] = 0;
  REP(i, n) {
    if (prime[i]) {
      dbg(i);
      for (ll j = i + i; j < n; j += i) {
        prime[j] = 0;
      }
    }
  }

  vi nl(n, 0);
  RREP(i, k) {
    nl[i] = modpow(k / (i + 1), n, MOD);
    for (ll j = 2; j * (i + 1) <= k; ++j) {
      nl[i] -= nl[j * (i + 1) - 1];
    }
    ans += (i + 1) * nl[i];
  }
  cout << ans << endl;
}