from itertools import combinations N, P, Q = map(int, input().split()) A = list(map(int, input().split())) A.sort() m = max(A) + 10 memo10 = [-1] * m memo9 = [-1] * m memo7 = [-1] * m memo5 = [-1] * m def f10(x): if memo10[x] != -1: return memo10[x] res = pow(10, x, P) memo10[x] = res return res def f9(x): if memo9[x] != -1: return memo9[x] res = pow(9, x, P) memo9[x] = res return res def f7(x): if memo7[x] != -1: return memo7[x] res = pow(7, x, P) memo7[x] = res return res def f5(x): if memo5[x] != -1: return memo5[x] res = pow(5, x, P) memo5[x] = res return res ans = 0 for a, b, c, d in combinations(A, 4): x = f10(a) x += f9(b) x += f7(c) x += f5(d) if x % P == Q: ans += 1 print(ans)