#include using namespace std; struct iofast_t { iofast_t() { ios::sync_with_stdio(false); cin.tie(nullptr); } } iofast; struct uns_t {} uns; template auto vec(Element init, Head arg, Args ...args) { if constexpr (sizeof...(Args) == 0) return std::vector(arg, init); else return std::vector(arg, vec(init, args...)); } template auto vec(uns_t, Head arg, Args ...args) { return vec(Element(), arg, args...); } template > T &chmin(T &l, T r, Compare &&f = less()) { return l = min(l, r, f); } template > T &chmax(T &l, T r, Compare &&f = less()) { return l = max(l, r, f); } constexpr int lim = 14; uint32_t dp[2][1 << lim][1000]; int main() { int n, k; cin >> n >> k; auto [c, count] = [&] { array c = {}; array count = {}; auto iter = begin(c); for (int i = 1; i <= 9; ++i) { int v; cin >> v; count[i - 1] = v; while (v--) { *iter++ = i; } } return make_tuple(c, count); }(); auto pow = [&] { array pow = {}; pow[0] = 1; for (int i = 1; i < n; ++i) { pow[i] = (pow[i - 1] * 10) % k; } return pow; }(); auto popcount = [](auto x) { return bitset(x).count(); }; bool turn = false; dp[turn][0][0] = 1; for (int i = 1; i <= n; ++i) { for (int to = 0; to < (1 << n); ++to) { if (i != popcount(to)) { continue; } for (int j = 0; j < n; ++j) { if (!(to & (1 << j))) { continue; } int from = to ^ (1 << j); for (int r = 0; r < k; ++r) { dp[!turn][to][r] += dp[turn][from][(k + (r - (pow[i - 1] * c[j])) % k) % k]; } } } turn = !turn; for (int j = 0; j < (1 << n); ++j) { for (int r = 0; r < k; ++r) { dp[!turn][j][r] = 0; } } } auto ans = dp[turn][(1 << n) - 1][0]; for (int i = 0; i < 9; ++i) { int64_t f = 1; for (int j = 1; j <= count[i]; ++j) { f *= j; } ans /= f; } cout << ans << endl; }