N,P,Q=map(int, input().split()) A=list(map(int, input().split())) A=sorted(A) ans=0 for i in range(N-3): a=pow(10,A[i],P) for j in range(i+1,N-2): b=pow(9,A[j],P) for k in range(j+1,N-1): c=pow(7,A[k],P) for l in range(k+1,N): d=pow(5,A[l],P) if (a+b+c+d)%P==Q: ans+=1 print(ans)