from itertools import combinations from functools import lru_cache N, P, Q = map(int, input().split()) A = sorted(map(int, input().split())) @lru_cache(10**7) def power(x, y): return pow(x, y, P) ans = 0 for a, b, c, d in combinations(A, r=4): if (power(10, a) + power(9, b) + power(7, c) + power(5, d)) % P == Q: ans += 1 print(ans)