对于置换0→i,1→i+1……,其中包含0的循环的元素个数显然是n/gcd(i,n),由对称性,循环节个数即为gcd(i,n)。
那么要求的即为Σngcd(i,n)/n(i=0~n-1,也即1~n)。考虑枚举gcd。显然gcd(i,n)=x在该范围内解的个数是φ(n/x)。分解一下质因数即可。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define P 1000000007
#define N 100
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<''||c>'')) c=getchar();return c;}
int gcd(int n,int m){return m==?n:gcd(m,n%m);}
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
int m,T,prime[N],cnt[N],p[N][N],t,ans;
int ksm(int a,int k)
{
int s=;
for (;k;k>>=,a=1ll*a*a%P) if (k&) s=1ll*s*a%P;
return s;
}
void dfs(int k,int s,int phi)
{
if (k>t) {ans=(ans+1ll*ksm(m,s-)*phi)%P;return;}
for (int i=;i<cnt[k];i++) dfs(k+,1ll*s*p[k][i]%P,1ll*phi*(prime[k]-)%P*p[k][cnt[k]-i-]%P);
dfs(k+,1ll*s*p[k][cnt[k]]%P,phi);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
T=read();
while (T--)
{
int n=read();m=n,t=;
for (int i=;i*i<=n;i++)
if (n%i==)
{
prime[++t]=i,cnt[t]=;n/=i;
while (n%i==) cnt[t]++,n/=i;
}
if (n>) prime[++t]=n,cnt[t]=;
for (int i=;i<=t;i++)
{
p[i][]=;
for (int j=;j<=cnt[i];j++) p[i][j]=1ll*p[i][j-]*prime[i]%P;
}
ans=;dfs(,,);
printf("%d\n",ans);
}
return ;
}