N,P,Q=map(int,input().split()) A=list(map(int,input().split())) s=set(A) ans=0 for i in range(N): for j in range(i+1,N): for k in range(j+1,N): for x in range(k+1,N): if (pow(10,A[i],P)+ pow(9,A[j],P)+ pow(7,A[k],P)+ pow(5,A[x],P))%P==Q: ans+=1 print(ans)