N, P, Q = map(int, input().split()) A = list(map(int, input().split())) dp = [[[0] * P for i in range(4)] for j in range(N + 1)] dp[0][0][0] = 1 p = [pow(5, A[i], P) for i in range(N)] for i in range(1, N + 1): a = pow(10, A[i - 1], P) b = pow(9, A[i - 1], P) c = pow(7, A[i - 1], P) for j in range(P): dp[i][0][j] += dp[i - 1][0][j] dp[i][1][(j + a) % P] += dp[i - 1][0][j] dp[i][1][j] += dp[i - 1][1][j] dp[i][2][(j + b) % P] += dp[i - 1][1][j] dp[i][2][j] += dp[i - 1][2][j] dp[i][3][(j + c) % P] += dp[i - 1][2][j] dp[i][3][j] += dp[i - 1][3][j] ans = 0 for i in range(4, N): ans += dp[i][-1][(Q - p[i]) % P] print(ans)