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