S = input R = range P = print def I(): return int(S()) def M(): return map(int, S().split()) def L(): return list(M()) def O(): return list(map(int, open(0).read().split())) def yn(b): print("Yes" if b else "No") biga = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" smaa = "abcdefghijklmnopqrstuvwxyz" n, p, q = M() a = L() a.sort() c = [0] * p ans = 0 for i in range(n): for j in range(i + 1, n): for k in range(j + 1, n): r = (q - pow(9, a[i], p) - pow(7, a[j], p) - pow(5, a[k], p)) % p ans += c[r] c[pow(10, a[i], p)] += 1 print(ans)