ll Gcd(ll a, ll b) { if (!b) return a; returnGcd(b, a % b); }
ll qpow(ll a, int b) { ll res = 1; while (b) { if (b & 1) res *= a, res %= Mod; a *= a;a %= Mod; b >>= 1; } return res; }
intget_phi(int n) { int ans = n; for (int i = 2; i <= n / i; ++ i) if (n % i == 0) { ans = ans / i * (i - 1); while (n % i == 0) n /= i; } if (n != 1) ans = ans / n * (n - 1); return ans; }
intmain() { int t, n;cin >> t; while (t --) { scanf("%d", &n); ll ans = 0; for (int i = 1; i <= n / i; ++ i) if (n % i == 0) { ans = (ans + qpow(n, i) * get_phi(n / i) % Mod) % Mod; if (i * i != n) ans = (ans + qpow(n, n / i) * get_phi(i) % Mod) % Mod; }
printf("%lld\n", ans * qpow(n, Mod - 2) % Mod); } return0; }
classMatrix{ public: int a[N][N], n, m; inlinevoidclean(int _n, int _m) { memset(a, 0, sizeof a); n = _n, m = _m; } const Matrix operator *(const Matrix &b)const{ Matrix c;c.clean(n, b.m); if (m != b.n) return c; for (int i = 1; i <= n; ++ i) for (int j = 1; j <= b.m; ++ j) for (int k = 1; k <= m; ++ k) c.a[i][j] = (c.a[i][j] + a[i][k] * b.a[k][j]) % Mod; } const Matrix operator ^(int x)const{ Matrix res, tmp = *this;res.clean(n, m); for (int i = 1; i <= n; ++ i) res.a[i][i] = 1; while (x) { if (x & 1) res = res * tmp; x >>= 1; tmp = tmp * tmp; } return res; } }F0, ed;
intget_phi(int n) { int ans = n; for (int i = 2; i <= n / i; ++ i) if (n % i == 0) { ans = ans / i * (i - 1); while (n % i == 0) n /= i; } if (n != 1) ans = ans / n *(n - 1); return ans; }
inlineintqpow(int a, int k) { int res = 1; while (k) { if (k & 1) res = res * a % Mod; a = a * a % Mod; k >>= 1; } return res; }
inlineintinv(int x){returnqpow(x, Mod - 2);}
inlineintcalc(int x) { ed = F0 ^ x; int sum = 0; for (int i = 1; i <= M; ++ i) sum += ed.a[i][i], sum %= Mod; return sum; }
intmain() { int t, n, k;cin >> t; while (t --) { cin >> n >> M >> k; F0.clean(M, M); for (int i = 1; i <= M; ++ i) for (int j = 1; j <= M; ++ j) F0.a[i][j] = 1;
while (k --) { int x, y; scanf("%d %d", &x, &y); F0.a[x][y] = F0.a[y][x] = 0; }
int res = 0; for (int i = 1; i <= n / i; ++ i) if (n % i == 0) { res = (res + calc(i) * (get_phi(n / i) % Mod) % Mod) % Mod; if (i == n / i) continue; res = (res + calc(n / i) * (get_phi(i) % Mod) % Mod) % Mod; } cout << res * inv(n) % Mod << endl; } return0; }