from collections import * n, p, q = map(int, input().split()) a = list(map(int, input().split())) num10, num9, num7, num5 = 1, 1, 1, 1 d10, d9, d7, d5 = defaultdict(int), defaultdict( int), defaultdict(int), defaultdict(int) for i in range(1, 2 * 10**6 + 2): num10 = num10 * 10 % p num9 = num9 * 9 % p num7 = num7 * 7 % p num5 = num5 * 5 % p d10[i] = num10 d9[i] = num9 d7[i] = num7 d5[i] = num5 a.sort() ans = 0 for i1 in range(n - 3): for i2 in range(i1 + 1, n - 2): for i3 in range(i2 + 1, n - 1): for i4 in range(i3 + 1, n): if (d10[a[i1]] + d9[a[i2]] + d7[a[i3]] + d5[a[i4]]) % p == q: ans += 1 print(ans)