cin >> n >> p; init_C(p); for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) { ll t = a[i]; for (int o = 0; o < N; ++o) { int v = t % p; t /= p; if (v) d[i].emplace_back(o, v); } } ll ans = 0; nxt[n] = n + 1; for (int i = n - 1; i; --i) nxt[i] = a[i + 1] ? i + 1 : nxt[i + 1]; for (int l = 1; l <= n; ++l) { int res = 1; vector<int> s(N); for (int r = l; r <= n; r = nxt[r]) { for (auto [k, v] : d[r]) { if (s[k] + v >= p) { res = 0; break; } s[k] += v; res = res * C[s[k]][v] % p; } if (!res) break; ans += (nxt[r] - r) * res; } } cout << ans << "\n"; return0; }