# [HDU 5780] gcd (公式证明)

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1000001;
const int p=1e9+7;
int T,x,n;
int phi[N];
bool b[N];

char ch=getchar();x=0;
for(;ch<'0'||ch>'9';ch=getchar());
for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
}

ll power(int a,int n){
ll ans=1;
for(ll sum=a;n;sum=sum*sum%p,n>>=1) if (n&1) ans=ans*sum%p;
return ans;
}

x+=y;
while(x<0) x+=p;
while(x>=p) x-=p;
}

x+=y;
while(x<0) x+=p;
while(x>=p) x-=p;
}

void init(){
for(int i=1;i<N;i++) phi[i]=i;
for(int i=2;i<N;i++)
if (!b[i]){
phi[i]=i-1;
for(int j=2;i*j<N;j++)
b[i*j]=1,phi[i*j]=phi[i*j]/i*(i-1);
}
}

int main(){
init();
for(int o=1;o<=T;o++){
if (x==1){ printf("0\n");continue; }
ll ans=0,inv=power(x-1,p-2);
for(int i=1,j=1;i<=n;i=j+1){
j=n/(n/i);
int w=n/i;
ll v=power(x,i)*(power(x,j-i+1)-1)%p*inv%p*phi[w]%p*2%p;
//cout<<i<<' '<<j<<' '<<power(x,i)<<' '<<(power(x,j-i+1)-1)*inv%p<<endl;
}
ans=(ans-(power(x,n)-1)*x%p*inv%p)%p;
printf("%lld\n",(ans+p)%p);
}
return 0;
}