WA on 3 5 6 8 10
#include<bits/stdc++.h>
using namespace std;
long long read()
{
long long s = 0,w = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-') w = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9')
{
s = (s << 1) + (s << 3) + (ch ^ 48);
ch = getchar();
}
return s * w;
}
const int N = 1e6 + 10;
long long T,x[N],n,m,n1,n2,P,cnt,mul = 1;
long long p[N],a[N];
long long quickpow(long long c,long long n,long long m)
{
long long res = 1;
while(n)
{
if(n & 1) res = res * c % m;
c = c * c % m;
n >>= 1;
}
return res;
}
void exgcd(long long a,long long b,long long &x,long long &y)
{
if(b == 0){x = 1;y = 0;return;}
exgcd(b,a % b,x,y);
long long t = x;x = y,y = t - (a / b) * y;
return;
}
long long inv(long long a,long long p)
{
long long x,y;
exgcd(a,p,x,y);
return (x % p + p) % p;
}
long long CRT()
{
long long res = 0;
for(int i = 1;i <= cnt;i++) mul *= p[i];
for(int i = 1;i <= cnt;i++) res += a[i] * inv(mul / p[i],p[i]) * mul / p[i];
return res % mul;
}
long long calc(long long n,long long x,long long P)
{
if(n == 0) return 1;
long long s = 1;
for(long long i = 1;i <= P;i++) if(i % x) s = s * i % P;
s = quickpow(s,n / P,P);
for(long long i = n / P * P + 1;i <= n;i++) if(i % x) s = i % P * s % P;
return s * calc(n / x,x,P) % P;
}
long long mulLucas(long long m,long long n,long long x,long long P)
{
long long c = 0;
for(long long i = m;i >= 1;i /= x) c += i / x;
for(long long i = n;i >= 1;i /= x) c -= i / x;
for(long long i = m - n;i >= 1;i /= x) c -= i / x;
return quickpow(x,c,P) % P * calc(m,x,P) % P * inv(calc(n,x,P),P) % P * inv(calc(m - n,x,P),P) % P;
}
long long exlucas(long long m,long long n,long long P)
{
memset(p,0,sizeof(p));
memset(a,0,sizeof(a));
cnt = 0,mul = 1;
if(m < n) return 0;
for(long long i = 2;i * i <= P;i++)
{
if(P % i == 0)
{
p[++cnt] = 1;
while(P % i == 0) p[cnt] = p[cnt] * i,P /= i;
a[cnt] = mulLucas(m,n,i,p[cnt]);
}
}
if(P > 1) p[++cnt] = P,a[cnt] = mulLucas(m,n,P,P);
return CRT();
}
int main()
{
// freopen("input.in","r",stdin);
T = read(),P = read();
while(T--)
{
n = read(),n1 = read(),n2 = read(),m = read();
for(int i = 1;i <= n1 + n2;i++) x[i] = read();
for(int i = 1;i <= n2;i++) m -= x[i + n1] - 1;
cout << (exlucas(m - 1,n - 1,P) % P + P) % P << endl;
}
return 0;
}