N,P,Q=map(int,input().split()) A=list(map(int,input().split())) A.sort() pow10=[pow(10,a,P) for a in A] pow9=[pow(9,a,P) for a in A] pow7=[pow(7,a,P) for a in A] pow5=[pow(5,a,P) for a in A] ans=0 for a in range(N): for b in range(a+1,N): for c in range(b+1,N): for d in range(c+1,N): if (pow10[a]+pow9[b]+pow7[c]+pow5[d])%P==Q: ans+=1 print(ans)