#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#include <time.h>
#define dibs reserve
#define OVER9000 1234567890123456789LL
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):x)
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge
typedef long long cat;
#ifdef DONLINE_JUDGE
// palindromic tree is better than splay tree!
#define lld I64d
#endif
inline cat pw(cat a, cat e, cat mod) {
cat x =a, ret =1;
while(e > 0) {
if(e&1) ret =(ret*x)%mod;
x =(x*x)%mod;
e /=2;
}
return ret;
}
cat mod =330301441;
cat w[11] ={0,0,330301440,93236247,39710288,161934030,93236248,7691605,90917647,25847905,7743336};
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(10);
int T;
cin >> T;
vector<cat> pw2(200000,1);
for(int i =1; i < 200000; i++) pw2[i] =(pw2[i-1]*2)%mod;
while(T--) {
int N,K;
cat X;
cin >> N >> K >> X;
vector<int> A(N);
for(int i =0; i < N; i++) {
cin >> A[i];
A[i] =min(N+1,A[i]);
}
// compute P1
vector<int> s(N+2,0);
for(int i =0; i < N; i++) s[A[i]]++;
int max_mex =0;
while(s[max_mex]) max_mex++;
vector<cat> prefprod(N+3,1), sufprod(N+3,1);
for(int i =1; i <= N+1; i++) prefprod[i] =prefprod[i-1]*(pw(2,s[i-1],mod)-1)%mod;
for(int i =N+1; i >= 0; i--) sufprod[i] =sufprod[i+1]*pw(2,s[i],mod)%mod;
vector<cat> P1(max_mex+1);
for(int i =0; i <= max_mex; i++) P1[i] =prefprod[i]*sufprod[i+1]%mod;
// normalize
cat sum =0;
for(int i =0; i <= max_mex; i++) sum +=P1[i];
sum =pw(sum%mod,mod-2,mod);
for(int i =0; i <= max_mex; i++) P1[i] =(sum*P1[i])%mod;
// multidimensional NTT
int D =0, Pw =1;
while(max_mex+1 >= Pw) Pw *=K, D++;
vector<cat> pwK(D+1,1);
for(int i =1; i <= D; i++) pwK[i] =(pwK[i-1]*K)%mod;
vector< vector<cat> > pwW(K,vector<cat>(K,1)); // powers of primitive root
for(int i =0; i < K; i++) for(int j =0; j < K; j++)
for(int k =0; k < (i*j); k++) pwW[i][j] =(pwW[i][j]*w[K])%mod;
vector< vector<cat> > pwWI(K,vector<cat>(K,1)); // powers of inverse primitive root
cat wi =pw(w[K],mod-2,mod);
for(int i =0; i < K; i++) for(int j =0; j < K; j++)
for(int k =0; k < (i*j); k++) pwWI[i][j] =(pwWI[i][j]*wi)%mod;
P1.resize(Pw,0);
vector<cat> FP1 =P1;
vector<cat> FP1_nxt(Pw,0); // after NTT-ing dimension d
for(int d =0; d < D; d++) {
for(int i =0; i < Pw; i++) {
int d0 =(i/pwK[d])%K;
int x =i-d0*pwK[d];
FP1_nxt[i] =0;
for(int j =0; j < K; j++)
FP1_nxt[i] +=FP1[x+j*pwK[d]]*pwW[d0][j];
}
for(int i =0; i < Pw; i++) FP1[i] =FP1_nxt[i]%mod;
}
// F[PX]
vector<cat> FPX =FP1;
X %=mod-1;
for(int i =0; i < Pw; i++) FPX[i] =pw(FP1[i],X,mod);
// inverse transform
vector<cat> PX =FPX;
vector<cat> PX_nxt(Pw,0); // after NTT-ing dimension d
for(int d =0; d < D; d++) {
for(int i =0; i < Pw; i++) {
int d0 =(i/pwK[d])%K;
int x =i-d0*pwK[d];
PX_nxt[i] =0;
for(int j =0; j < K; j++)
PX_nxt[i] +=PX[x+j*pwK[d]]*pwWI[d0][j];
}
for(int i =0; i < Pw; i++) PX[i] =PX_nxt[i]%mod;
}
// compute the output hash
cat ans =0, inv =pw(Pw,mod-2,mod);
for(int i =0; i < Pw; i++) {
if(PX[i] == 0 && i > 0) continue;
PX[i] =(PX[i]*inv)%mod; // NTT wasn't normalised correctly
cat q =1LL*i*i%mod*PX[i]%mod*PX[i]%mod*PX[i]%mod;
ans +=pw(q,i,mod);
}
ans %=mod;
if(ans < 0) ans +=mod;
cout << ans << "\n";
}
}
// look at my code
// my code is amazing
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Ci8vIGlvc3RyZWFtIGlzIHRvbyBtYWluc3RyZWFtCiNpbmNsdWRlIDxjc3RkaW8+Ci8vIGJpdGNoIHBsZWFzZQojaW5jbHVkZSA8aW9zdHJlYW0+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxjc3RkbGliPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8c2V0PgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZSA8cXVldWU+CiNpbmNsdWRlIDxzdGFjaz4KI2luY2x1ZGUgPGxpc3Q+CiNpbmNsdWRlIDxjbWF0aD4KI2luY2x1ZGUgPGlvbWFuaXA+CiNpbmNsdWRlIDx0aW1lLmg+CiNkZWZpbmUgZGlicyByZXNlcnZlCiNkZWZpbmUgT1ZFUjkwMDAgMTIzNDU2Nzg5MDEyMzQ1Njc4OUxMCiNkZWZpbmUgQUxMX1RIRShDQUtFLExJRSkgZm9yKGF1dG8gTElFID1DQUtFLmJlZ2luKCk7IExJRSAhPSBDQUtFLmVuZCgpOyBMSUUrKykKI2RlZmluZSB0aXNpYyA0NwojZGVmaW5lIHNvY2xvc2UgMWUtOAojZGVmaW5lIGNob2NvbGF0ZSB3aW4KLy8gc28gbXVjaCBjaG9jb2xhdGUKI2RlZmluZSBwYXRrYW4gOQojZGVmaW5lIGZmIGZpcnN0CiNkZWZpbmUgc3Mgc2Vjb25kCiNkZWZpbmUgYWJzKHgpICgoeCA8IDApPy0oeCk6eCkKI2RlZmluZSB1aW50IHVuc2lnbmVkIGludAojZGVmaW5lIGRibCBsb25nIGRvdWJsZQojZGVmaW5lIHB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