from itertools import combinations N,P,Q = map(int,input().split()) A = sorted(list(map(int,input().split()))) ans = 0 for x in combinations(A,4): if (pow(10,x[0],P)+pow(9,x[1],P)+pow(7,x[2],P)+pow(5,x[3],P))%P==Q: ans += 1 print(ans)