n,p,q=map(int,input().split()) a=list(map(int,input().split())) a.sort() r=[[0]*p for i in range(n)] for i in range(n): for j in range(i+1): r[j][pow(5,a[i],p)]+=1 g=0 for i in range(n-3): for j in range(i+1,n-2): for k in range(j+1,n-1): g+=r[k+1][(q-pow(10,a[i],p)-pow(9,a[j],p)-pow(7,a[i],p))%p] print(g)