#include<bits/stdc++.h>
using namespace std;
#define
#define
#define
#include <iostream>
ll q_mul(ll a, ll b, ll mod) {
ll ans = 0;
a %= mod;
while (b) {
if (b & 1)
ans = (ans + a) % mod;
a = (a + a) % mod;
b >>= 1;
}
return ans % mod;
}
ll pow_mod(ll a, ll b, ll n) {
ll ans = 1,t = a % n;
while (b) {
if (b & 1)
ans = q_mul(ans, t, n) % n;
t = q_mul(t, t, n) % n;
b = b >> 1;
}
return ans;
}
bool isprime(ll n) {
ll pan[10] = {2, 3, 5, 7, 11, 31, 61, 73, 233, 331};
ll i;
for (i = 0; i < 10; i++)
if (pow_mod(pan[i], n - 1, n) != 1)
break;//if(pan[i]^(n-1)%n==1)this int is prime
if (i == 10)
return true;
else
return false;
}
int main() {
ll ans, p;
int t;
while (~scanf("%d", &t)) {
while (t--) {
scanf("%lld", &p);
ans = 1;
for (ll i = p - 1; i > 0; i--) {
if (isprime(i)) {
break;
} else {
ans = q_mul(ans, i, p);
}
}
ans = pow_mod(ans, p - 2, p);
ans = q_mul(p-1,ans,p);
printf("%lld\n", ans);
}
}
return 0;
}