HDU-6265 Master of Phi

%20%5Ccdot%20%5Cfrac%7Bn%7D%7Bd%7D%20&preview=true)

%20%5Ccdot%20%5Cfrac%7Bn%7D%7Bd%7D%20%0A%5C%5C%0A%3Dn%5Ccdot%20%5Cprod_%7Bp%5Ea%7C%7Cn%7D%5Csum_%7Bk%3D0%7D%5E%7Ba%7D%5Cfrac%7B%5Cvarphi%20(p%5Ek)%7D%7Bp%5Ek%7D%0A%5C%5C%0A%3Dn%5Ccdot%20%5Cprod_%7Bp%5Ea%7C%7Cn%7D(1%2B%5Csum_%7Bk%3D1%7D%5E%7Ba%7D%5Cfrac%7B(p%5Ek-p%5E%7Bk-1%7D)%7D%7Bp%5Ek%7D)%0A%5C%5C%0A%3D%5Cprod_%7Bp%5Ea%7C%7Cn%7Dp%5E%7Ba-1%7D%5Ccdot(p%5Ccdot%20a%2Bp-a)&preview=true)

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
const ll mod=998244353;
ll quick(ll p,ll a)
{
ll ans=1;
while(a)
{
if(a&1)
ans=ans*p%mod;
a>>=1;
p=p*p%mod;
}
return ans%mod;
}
int main()
{
ll p[25],q[25];
ll t;cin>>t;
while(t--)
{
ll m;
cin>>m;
for(ll i=1;i<=m;i++)
{
scanf("%lld%lld",p+i,q+i);
}
ll ans=1;
for(ll i=1;i<=m;i++)
{
ll mi=quick(p[i],q[i]-1);
ans=ans*(p[i]*q[i]%mod+p[i]-q[i]+mod)%mod*mi%mod;
}
cout<<(ans+mod)%mod<<endl;
}
}