voidadd(const digit& rhs){ for (int i = 0; i < std::max(cnt, rhs.cnt); i++) { d[i] += rhs.d[i]; if (d[i] >= P) { d[i] -= P; d[i + 1]++; } } } };
intmain(){ std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); int n, p; std::cin >> n >> p; P = 1; while (P < 5000)P *= p; P /= p; std::vector<std::vector<int>> C(P + 1, std::vector<int>(P + 1)); C[0][0] = 1; for (int i = 1; i <= P; ++i) { C[i][0] = C[i][i] = 1; for (int j = 1; j < P; ++j) { C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % p; } } auto lucas = [&](const digit& n, const digit& m) { int s = 1; for (int i = 0; i < m.cnt; ++i) { s *= C[n.d[i]][m.d[i]] %= p; } return s; }; std::vector<digit> a(n + 1); for (int i = 0; i < n; ++i) { ll x; std::cin >> x; a[i] = digit(x); }
std::vector<int> r(n + 1); a[n].cnt = 1; int st = 0; for (int i = 0; i <= n; i++) { if (a[i].cnt) { for (int j = i - 1; j >= st; j--)r[j] = i; st = i; } }
ll ans = 0; for (int i = 0; i < n; ++i) { int ptr = i; digit tmp = a[ptr]; int cur = 1; while (cur && ptr < n) { ans += (r[ptr] - ptr) * cur; ptr = r[ptr]; tmp.add(a[ptr]); cur *= lucas(tmp, a[ptr]); cur %= p; } } std::cout << ans << '\n'; return0; }