import sys #sys.setrecursionlimit(10 ** 6) #sys.set_int_max_str_digits(0) INF = float('inf') MOD = 10**9 + 7 MOD2 = 998244353 from collections import defaultdict def ceil(A,B): return -(-A//B) def ceil(A,B): return -(-A//B) def solve(): def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LC(): return list(sys.stdin.readline().rstrip()) def IC(): return [int(c) for c in sys.stdin.readline().rstrip()] def MI(): return map(int, sys.stdin.readline().split()) N,P,Q = MI() A = LI() A.sort() D = [defaultdict(lambda:0) for i in range(N)] for c in range(N): for d in range(c+1,N): D[c][pow(5,A[d],P)]+=1 Ans = 0 for a in range(N): i = pow(10,A[a],P) for b in range(a+1,N): j = pow(9,A[b],P) for c in range(b+1,N): k = pow(7,A[c],P) ijk = (i+j+k)%P w = Q-ijk w%=P Ans+=D[c][w] print(Ans) return solve()