N,P,Q=map(int,input().split()) A=list(map(int,input().split())) L=[[0 for _ in range(4)]for _ in range(N)] t=[10,9,7,5] for i in range(N): for j in range(4): L[i][j]=pow(t[j],A[i],P) ans=0 for i in range(N): for j in range(i+1,N-3): for k in range(j+1,N-2): for m in range(k+1,N-1): if (L[i][0]+L[j][1]+L[k][2]+L[m][3])%P==Q: ans+=1 print(ans)