N, P, Q = map(int, input().split(" ")) A = sorted(list(map(int, input().split(" ")))) def mod_pow(a, n): res = 1 while n > 0: if n & 1: res = res * a % P a = a * a % P n >>= 1 return res dval_cnt = [[0 for _ in range(P)] for _ in range(N)] for n in range(N): dval = mod_pow(5, A[n]) for k in range(n+1): dval_cnt[k][dval] += 1 ans = 0 for ai in range(N-3): for bi in range(ai+1, N-2): for ci in range(bi+1, N-1): abcval = (mod_pow(10, A[ai]) + mod_pow(9, A[bi]) + mod_pow(7, A[ci])) % P ans += dval_cnt[ci+1][(Q - abcval) % P] print(ans)