Problem p02715 (C++)

Buggy submission (status: Runtime Error):
/*
Arthor : Ender_zzm
E-mail zzm_ender_wiggin@outlook.com
Blog oi.ender-zzm.pro
*/
#include <bits/stdc++.h>
using namespace std;

template <class T> inline void read(T &a) {
  register T x = 0, flag = 1;
  register char ch;
  while (!isdigit(ch = getchar()))
    if (ch == '-')
      flag = -1;
  while (x = x * 10 + (ch & 15), isdigit(ch = getchar()))
    ;
  a = x * flag;
}

template <class T> inline void write(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}

template <class T> inline bool Chkmax(T &x, const T &y) {
  return x < y ? x = y, true : false;
}
template <class T> inline bool Chkmin(T &x, const T &y) {
  return x > y ? x = y, true : false;
}

#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Rep(i, a, b) for (int i = (a); i >= (b); i--)
#define For_edge(x) for (int i = head[x]; i; i = net[i])
#define mem0(a) memset(a, 0, sizeof a)

#define int long long
const int maxn = 501000;
const int Mod = 1e9 + 7;
int n, k;
int prime[maxn], tot, vis[maxn], phi[maxn];
int S[maxn];
int Get_phi(int n) {
  phi[1] = 1;
  For(i, 2, n) {
    if (vis[i] == 0) {
      prime[++tot] = i;
      phi[i] = i - 1;
    }
    For(j, 1, tot) {
      int v = i * prime[j];
      if (v > n)
        break;
      vis[v] = 1;
      if (i % prime[j] == 0) {
        phi[v] = phi[i] * prime[j];
        break;
      }
      phi[v] = phi[i] * (prime[j] - 1);
    }
  }
  For(i, 1, n) { S[i] = (S[i - 1] + phi[i]) % Mod; }
}
int Pow(int x, int y) {
  int ans = 1;
  while (y) {
    if (y & 1)
      ans = 1ll * ans * x % Mod;
    x = 1ll * x * x % Mod;
    y >>= 1;
  }
  return ans;
}
signed main() {
  int ans = 0;
  cin >> n >> k;
  Get_phi(k);
  for (int l = 1, r = 0; l <= k; l = r + 1) {
    r = min(k, k / (k / l));
    ans =
        (ans + 1ll * (S[r] - S[l - 1] + Mod) % Mod * Pow(k / l, n) % Mod) % Mod;
  }
  cout << ans;
  return 0;
}


Fixed submission:
/*
Arthor : Ender_zzm
E-mail zzm_ender_wiggin@outlook.com
Blog oi.ender-zzm.pro
*/
#include <bits/stdc++.h>
using namespace std;

template <class T> inline void read(T &a) {
  register T x = 0, flag = 1;
  register char ch;
  while (!isdigit(ch = getchar()))
    if (ch == '-')
      flag = -1;
  while (x = x * 10 + (ch & 15), isdigit(ch = getchar()))
    ;
  a = x * flag;
}

template <class T> inline void write(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}

template <class T> inline bool Chkmax(T &x, const T &y) {
  return x < y ? x = y, true : false;
}
template <class T> inline bool Chkmin(T &x, const T &y) {
  return x > y ? x = y, true : false;
}

#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Rep(i, a, b) for (int i = (a); i >= (b); i--)
#define For_edge(x) for (int i = head[x]; i; i = net[i])
#define mem0(a) memset(a, 0, sizeof a)

#define int long long
const int maxn = 501000;
const int Mod = 1e9 + 7;
int n, k;
int prime[maxn], tot, vis[maxn], phi[maxn];
int S[maxn];
void Get_phi(int n) {
  phi[1] = 1;
  For(i, 2, n) {
    if (vis[i] == 0) {
      prime[++tot] = i;
      phi[i] = i - 1;
    }
    For(j, 1, tot) {
      int v = i * prime[j];
      if (v > n)
        break;
      vis[v] = 1;
      if (i % prime[j] == 0) {
        phi[v] = phi[i] * prime[j];
        break;
      }
      phi[v] = phi[i] * (prime[j] - 1);
    }
  }
  For(i, 1, n) { S[i] = (S[i - 1] + phi[i]) % Mod; }
}
int Pow(int x, int y) {
  int ans = 1;
  while (y) {
    if (y & 1)
      ans = 1ll * ans * x % Mod;
    x = 1ll * x * x % Mod;
    y >>= 1;
  }
  return ans;
}
signed main() {
  int ans = 0;
  cin >> n >> k;
  Get_phi(k);
  for (int l = 1, r = 0; l <= k; l = r + 1) {
    r = min(k, k / (k / l));
    ans =
        (ans + 1ll * (S[r] - S[l - 1] + Mod) % Mod * Pow(k / l, n) % Mod) % Mod;
  }
  cout << ans;
  return 0;
}