Help me in solving ALTARAY problem

My issue

how the order decided in this of output

My code

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	
	return 0;
}

Problem Link: ALTARAY Problem - CodeChef

@deepakjdh31
U have to find the longest subarray for each i , including the ith index where each adjacent element are alternate.
Hint :- u have to iterate it from backwards.

plz, can you resolve my doubt to calculate multiplacative invere .i search many websites & everytime i not get exact sol of this.

#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

@deepakjdh31
Can u plzz explain your logic .

Hope u get it.