Help me in solving SPCHEF problem

My issue

plz resolve the issue in multiplicative inverse

My code

#include <bits/stdc++.h>
#define ll long long int
using namespace std;

 ll modPow(ll a,ll b,ll M){
     a%=M;
     ll ans=1;
     while(b){
         if(b&1){
         ans=((ans%M)*(a%M))%M;}
         a=((a%M)*(a%M))%M;
         b>>=1;
     }
     return ans;
 }

int main() {
	// your code goes here
	 ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	ll t;
	cin>>t;
	
	while(t--){
	    ll n,k,M;
	    cin>>n>>k>>M;
	    // gp series sum 
	    ll a=modPow(k,n,M)-1;
	    ll q=((a* 1LL *modPow(k-1,M-2,M))%M)%M;
	   cout<<q<<" "<<modPow(k,n,M)<<endl;
	}
	
	return 0;
}

Problem Link: SPCHEF Problem - CodeChef