#include using namespace std; #include using namespace atcoder; #define int long long void cumsum(std::vector& x, int d = 1) { for (int i = d; i < x.size(); i++) { x[i] += x[i - d]; } } long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } signed main() { int N, P; std::cin >> N >> P; int ans = 0; std::vector f = {0, 0}; int fact = 1; for (int i = 2; i <= N; i++) { f.push_back((modpow(N, N - i, P) * fact) % P); fact *= N - i; fact %= P; } std::vector dist(N, 0); int cumt = 0; int pat = 0; for (int i = N - 2; i > 0; i--) { cumt += f[i + 2]; pat += cumt; cumt %= P; pat %= P; dist[i] = pat; } int backet = 100; std::vector pats(N * 2, 0); std::vector> det(backet + 1, std::vector(N * 2, 0)); for (int i = 1; i <= N; i++) { for (int j = 0; j <= N; j++) { if (i * j >= N) { break; } if (j == 0) { pats[1] += N - i + 1; pats[i * (j + 1)] -= N - i + 1; continue; } if (j > backet) { int cnt = 0; for (int k = i * j + 1; k < i * (j + 1); k += j) { pats[k] += 1; cnt += 1; } pats[i * (j + 1)] -= cnt; } else { det[j][i * j + 1] += 1; int cnt = (i * (j + 1) - 2) / j - i + 1; det[j][std::min(N * 2 - 1, i * j + 1 + cnt * j)] -= 1; pats[i * (j + 1)] -= cnt; } } } for (int i = 1; i <= backet; i++) { cumsum(det[i], i); for (int j = 0; j < N * 2; j++) { pats[j] += det[i][j]; } } cumsum(pats); for (int i = 0; i < N - 1; i++) { pats[i] = N * (N + 1) / 2 - pats[i]; ans += pats[i] * dist[i]; ans %= P; } cumsum(f); std::vector> div(N + 1, std::vector()); std::vector> cum(N + 1, std::vector(1, 0)); for (int i = 1; i <= N; i++) { for (int j = i; j <= N; j += i) { div[j].push_back(i); cum[i].push_back(cum[i].back() + f[j]); } } for (int i = 1; i <= N; i++) { std::vector gcd(div[i].size(), 0); for (int j = div[i].size() - 1; j >= 0; j--) { gcd[j] += N / div[i][j]; for (int k = j - 1; k >= 0; k--) { if (div[i][j] % div[i][k] == 0) { gcd[k] -= gcd[j]; } } int pl = cum[div[i][j]].back() - cum[div[i][j]][i / div[i][j]]; int plcnt = cum[div[i][j]].size() - 1 - i / div[i][j]; int mi = cum[div[i][j]].back() - cum[div[i][j]][i / div[i][j] - 1] + f[i - 1] * (i / div[i][j] - 1); int micnt = plcnt + 1 + i / div[i][j] - 1; ans += (pl + f.back() * (micnt - plcnt) - mi) * gcd[j]; if (j == 0) { ans += (pl + f.back() * (micnt - plcnt) - mi) * N * (N - 1) / 2; } ans %= P; } } std::cout << (ans * N * (N - 1)) % P << std::endl; return 0; }