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 ten=[1] nin=[1] sev=[1] for i in range(1,2000001): ten+=[ten[-1]*10%p] nin+=[nin[-1]*9%p] sev+=[sev[-1]*7%p] 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-ten[a[i]]-nin[a[j]]-sev[a[k]])%p] print(g)