Not able to find the issue for my solution to MEXUM (april lunchtime)

#include
#include
#include
#include
using namespace std;

typedef long l ;
typedef vector vi;
typedef vector vl;
template<typename… T>
void read(T&… args){
((cin>>args), …);
}

#define ford(i,n) for(int i=0;i<n;i++)
#define fore(i,n,m) for(int i=n;i<=m;i++)

#define pb push_back
#define all(x) x.begin(),x.end()

#define deb(x) cout<<#x<<" "<<x<<’\n’;
#define maxi 998244353;

l pow(int a, int b){
if(b==0)
return 1;
return (a*pow(a,b-1))%maxi;
}

int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
read(t);
ford(q,t){
int n;
read(n);
vl a;
ford(i,n){
l temp;
read(temp);
a.pb(temp);
}
sort(all(a));
int k=a[0];
ford(i,n-1){
if(a[i]!=a[i+1]){
if(a[i]+1==a[i+1])
k = a[i+1];
else{
k = a[i];
break;
}
} else {
k = a[i];
}
}
if(a[0] != 1){
cout<<pow(2,n)<<’\n’;
continue;
}
if(k==1 && a[n-1]==1){
cout<<pow(2,n+1)-1<<’\n’;
continue;
}
vl frq;
int ptr=0;

int fcount=1;

while(a[ptr] <= k && ptr-1<n){
  if(a[ptr]!=a[ptr+1]){
      frq.pb(fcount);
      fcount=1;
  } else {
    fcount++;
  }
  ptr++;
}

l temp = 1;
l count = frq[0];;
l sum = pow(2,n-count);
  fore(i,2,k){
    temp = (temp*(pow(2,frq[i-2])-1))%maxi;
    count += frq[i-1];
    sum += (i * temp * pow(2,n-count))%maxi;
  }

temp = (temp*(pow(2,frq[k-1])-1))%maxi;
sum += ((k+1)*temp*pow(2,n-count))%maxi;
cout<<sum<<"\n";

}
return 0;
}

Thank you for your time.

Your solution would be giving TLE I suppose.

Your solution has a complexity of O(n^2) since you are finding power in linear time.
Read about binary exponentiation, it calculates power in logarithmic time.

Yes, the second case was coming TLE, but the first case came out to be WA. I am pretty sure the logic matches with the ones given by others in discussion but I am not able to figure out where my code fails.

Will definitely do that, thank you