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):#前計算 O(800*powのやつ) 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): for k in range(j+1,N): for m in range(k+1,N): print(i,j,k,m) if ( L[i][0]+ L[j][1]+ L[k][2]+ L[m][3] )%P==Q: ans+=1 print(ans)