import typing import sys # sys.setrecursionlimit(100100) # from collections import defaultdict input = lambda: sys.stdin.readline().strip() inf = 10**18 mod = 998244353 # import heapq def solve(): N, P, Q = map(int, input().split()) A = list(map(int,input().split())) A.sort() # lft = [0 for _ in range(P)] rht = [0 for _ in range(P)] # 最初 b = 2から lft[(pow(10, A[0], P)+pow(9, A[1], P))%P] = 1 # このとき c=3以降 for c in range(2, N): for d in range(c+1, N): rht[(pow(7, A[c], P)+pow(5, A[d], P))%P] += 1 ans = 0 for p in range(P): ans += lft[p]*rht[(Q-p)%P] for b in range(2, N-2): # b終わりを追加 lft = [0 for _ in range(P)] for a in range(b): lft[(pow(10, A[a], P)+pow(9, A[b], P))%P] += 1 # bはじまりを削除 for d in range(b+1, N): rht[(pow(7, A[b], P)+pow(5, A[d], P))%P] -= 1 for p in range(P): ans += lft[p]*rht[(Q-p)%P] print(ans) def main(): t = 1 for _ in range(t): solve() main()