Domain: Algorithms & Data Structures

You are given a multiset `a` of size `n`: `a_1, a_2, ..., a_n`.

You generate a new multiset `s` as follows:

1. Partition `a` into any number of **non-empty** multisets `x_1, x_2, ..., x_k`, using every element exactly once.
2. For each `x_i`, choose **one of its modes** and insert it into `s`.

A **mode** of a multiset is any element with maximum frequency in that multiset (ties allowed).

Count how many **different multisets** `s` can be produced (multiplicities matter), modulo `998244353`.

### Input
- `n` (`1 ≤ n ≤ 5000`)
- `a_1, ..., a_n` (`1 ≤ a_i ≤ n`)

### Output
Output the number of obtainable multisets `s` modulo `998244353`.

Grading rubric:
criterion: Does the answer define cnt[v] as the frequency of value v in the input multiset a?
weight: 10
criterion: Does the answer state the feasibility condition for a chosen support T (values appearing at least once in s) as: for every x ∉ T, cnt[x] ≤ sum_{c∈T}?
weight: 10
criterion: Does the answer simplify feasibility to the single inequality sum_{c∈T}?
weight: 10
criterion: Does the answer state that for any feasible support T, the multiplicity of each c∈T in output multiset s can be any integer t_c with 1 ≤ t_c ≤ cnt[c], giving exactly cnt[c] choices per c?
weight: 10
criterion: Does the answer define a knapsack DP dp[sum] that equals the total number of outputs whose chosen support has total weight sum = sum_{c∈T}?
weight: 10
criterion: Does the answer give the per-color DP transition: dp_next[s] += dp[s] (exclude), and dp_next[s+cnt] += dp[s] * cnt (include), all modulo 998244353?
weight: 10
criterion: Does the answer compute the final result as ans = sum_{s = M..n}?
weight: 10
criterion: Does the answer explicitly state the modulus as 998244353 and apply it to DP and final answer?
weight: 10
criterion: Does the answer state the overall time complexity as O(n^2) and space complexity as O(n) (or O(n^2) if explicitly chosen), consistent with n ≤ 5000?
weight: 20

Answer:
### Expert Solution

Let `cnt[v]` be the frequency of value `v` in the input multiset `a`, and let
`M = max_v cnt[v]`
Also let `MOD = 998244353`.

---

## 1) Feasibility condition

Let `T` be the **support** of the output multiset `s` (the set of values that appear at least once in `s`).

For any value `x ∉ T`, we must be able to place all `cnt[x]` copies of `x` into parts whose **chosen mode** is some `c ∈ T`, without making `x` the *unique* most frequent element in any part.

If a part’s chosen mode is `c`, then that part can contain at most as many `x`'s as it contains `c`'s (otherwise `x` would be strictly more frequent and thus the unique mode). Summing over all parts whose chosen mode lies in `T`, the total "hiding capacity" for `x` is at most `∑_{c ∈ T} cnt[c]`. Hence a necessary condition is:
`cnt[x] ≤ ∑_{c ∈ T} cnt[c]` for every `x ∉ T`.

This condition is also sufficient (we can distribute occurrences of each excluded `x` across the mode-parts so that in every part, `#x ≤ #(mode)`).

Since `cnt[x] ≤ M` for all `x`, this is equivalent to the single inequality:
`∑_{c ∈ T} cnt[c] ≥ M`

---

## 2) Counting multiplicities of `s` for a fixed support

If `c ∈ T`, then `c` must be chosen as a mode for some positive number `t_c` of parts.
Each such part must contain at least one `c`, so
`1 ≤ t_c ≤ cnt[c]`
Thus for each `c ∈ T` there are exactly `cnt[c]` choices for `t_c`, and for fixed support `T` the number of different multisets `s` is:
`∏_{c ∈ T} cnt[c]`

So the required answer is:
`∑_{T: ∑ cnt[c] ≥ M} ( ∏_{c ∈ T} cnt[c] ) (mod MOD)`

---

## 3) Knapsack DP

Let `dp[S]` be the total contribution (sum of products `∏ cnt[c]`) over supports `T` whose total weight
`S = ∑_{c ∈ T} cnt[c]`
equals `S`.

Initialize `dp[0] = 1`. For each value `v` with `cnt = cnt[v] > 0`:
- Exclude `v`: `dp_next[s] += dp[s]`
- Include `v`: `dp_next[s + cnt] += dp[s] · cnt`

All updates are modulo `998244353`.

Finally,
`ans = ∑_{s=M..n} dp[s] (mod 998244353)`

---

## 4) Complexity

The DP runs over sums `0...n` for at most `n` distinct values, so:
- Time: `O(n^2)`
- Memory: `O(n)`

This fits for `n ≤ 5000`.