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_set = [set([mod_pow(5, A[-1])])] for i in range(1, N): dval_set.append(dval_set[i-1] | set([mod_pow(5, A[N-i-1])])) dval_set = dval_set[::-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 if (Q - abcval) % P in dval_set[ci+1]: ans += 1 print(ans)