import sys input = sys.stdin.readline N,P,Q=map(int,input().split()) A=list(map(int,input().split())) A.sort() ANS=0 for i in range(N): for j in range(i+1,N): for k in range(j+1,N): for l in range(k+1,N): a=A[i] b=A[j] c=A[k] d=A[l] if (pow(10,a,P)+pow(9,b,P)+pow(7,c,P)+pow(5,d,P))%P==Q: ANS+=1 print(ANS)