We need to understand the problem: Given a sequence a of length n, each element between 1 and c. Define density of a sequence (c-array) as the maximal non-negative integer p such that any c-array (i.e., any sequence of positive integers of length p with each element between 1 and c) appears as a subsequence (not necessarily contiguous, but order preserved) of the given sequence.
In other words, the density p is the largest integer such that the set of all possible sequences of length p (with elements from 1..c) are all subsequences of the given array.
This is reminiscent of universality or completeness: The sequence is universal for all sequences of length p. For p=0, vacuously true (empty sequence) => density at least 0 always. For p=1, we need all singletons 1..c to appear as elements (i.e., each number 1..c appears at least once) to have density >=1. But also for density >=2, we need that for every pair (x,y) (ordered), there exists indices i<j such that a_i=x, a_j=y. That's a stronger condition. So density is essentially the largest p such that the sequence contains all possible sequences of length p as subsequences. This is exactly the concept of "sequence containing all permutations"? Not permutations but all sequences over an alphabet of size c of length p. This is similar to "universal sequence" or "superpermutation" but not exactly minimal.
Given a sequence a, we can compute its density p. It is defined as maximal p such that for any sequence b of length p over [c], b is a subsequence of a.
Observation: This property is monotonic: if a contains all sequences of length p, then it certainly contains all sequences of length p-1 (by ignoring last element). So density is well-defined.
Now, the problem: For each p from 0 to n, we need to count the number of subsequences (by selecting indices, i.e., any subset of positions, preserving order) of the original array such that the resulting array (the subsequence) has density exactly p. Note: The subsequence length k can be anything from 1 to n, and we consider all non-empty subsets? Actually the statement: "For all 0 ≤ p ≤ n find the number of sequences of indices 1 ≤ i_1 < i_2 < … < i_k ≤ n for all 1 ≤ k ≤ n, such that density of array a_{i_1}, a_{i_2}, …, a_{i_k} is equal to p." This means we count all non-empty subsequences (any non-empty subset of indices) of the original array, and for each we compute its density, and then we count how many have density p. The output includes s_0 as well, which corresponds to empty subsequence? But they said sequences of indices for all 1 ≤ k ≤ n, so non-empty; but they output s_0 also. Could be that empty subsequence is considered? The definition: "for all 0 ≤ p ≤ n find the number of sequences of indices ... for all 1 ≤ k ≤ n" That seems contradictory: they say "for all 0 ≤ p ≤ n find the number of sequences of indices ... for all 1 ≤ k ≤ n" meaning maybe they count all non-empty subsequences, and also consider p=0? But a non-empty subsequence could have density 0 if it doesn't contain all numbers 1..c? Wait, p=0 is always possible because any array (including empty) has density at least 0. But p=0 means maximal p is exactly 0, i.e., the array does NOT have density >=1. That is, it does NOT contain all numbers from 1 to c. So for non-empty sequences, density could be 0 if they miss at least one symbol. So s_0 counts the number of non-empty subsequences whose density is 0, plus maybe the empty subsequence? But the empty subsequence's density is 0 (since no p>0 can be satisfied). Usually they might include empty subsequence? Let's check examples:
Example1: n=4, c=1, array all ones.
c=1, so alphabet size 1.
We need to count subsequences. All subsequences (including empty?) They output "0 4 6 4 1". For p=0:0, p=1:4, p=2:6, p=3:4, p=4:1. Let's compute number of subsequences. Total non-empty subsets: 2^4 -1 = 15. They output sum = 0+4+6+4+1 = 15. So they counted only non-empty? But they also have p up to n=4, and s_4=1 corresponds to the full array. And s_0=0. So they didn't count empty subsequence. Indeed empty subsequence would have density 0, would add 1 to s_0, making total 16. So empty is not counted. So we count all non-empty subsequences.
Thus our task: Given n up to 3000, c up to 3000, we need to count, for each p, the number of non-empty subsequences (by picking any subset of indices) such that the density of the subsequence is exactly p.
We need to compute s_p mod 998244353.
Brute force over all subsets impossible.
We need to characterize density of a subsequence in terms of its structure.
Let's think about the condition that a sequence contains all length-p sequences over alphabet [c] as subsequences. This is equivalent to the sequence having "subsequence universality" of order p. There's known combinatorial characterizations: This is equivalent to the condition that for each symbol x, the number of occurrences? Actually, necessary and sufficient conditions: For each ordered tuple (x1,...,xp), there exists indices i1<...<ip with a_i1=x1,...,a_ip=xp. That's a huge condition. But perhaps there is a simpler characterization via the concept of "embedding" or "profile". Might relate to the property of being "p-superuniversal".
Think about the concept of "shuffle" or "containing all words". For a given sequence, when does it contain all words of length p? This is reminiscent of "De Bruijn sequence" but for subsequences not substrings.
Alternatively, consider the following: The set of all sequences of length p over alphabet c can be seen as the c-ary tree of depth p. A sequence S contains a word w if w is a subsequence. That's essentially saying that S is "rich" enough to have many different subsequences.
There is known concept: The maximum p such that S contains all length-p sequences is equal to the minimum over all symbols? Actually, it's related to the "subsequence density" or "combinatorial richness". Let's analyze.
Define for each position i, and each symbol x, the next occurrence after i, etc. The ability to embed arbitrary sequences is like: For each pattern, we need to be able to select positions matching that pattern in order. This is similar to the property of being "universal" for subsequences. It can be characterized by the length of the longest "avoidable" pattern? Actually, density p is the largest such that all patterns appear. That is equivalent to: p is the largest integer such that for every word w of length p, w is a subsequence. So p+1 is the smallest length such that there exists some word of length p+1 that is not a subsequence. So we need to find, for a given subsequence (selected from original), the length of the shortest missing pattern.
We need to count all subsequences by this missing pattern length (or by maximum p).
This looks like a counting problem that might be tackled with inclusion-exclusion or DP over the original array, maybe using automaton that tracks the "state" of which patterns have been covered? But number of patterns is huge (c^p), impossible for p up to n=3000.
Thus there must be a simpler characterization of density.
Observation: For a sequence to contain all words of length p, it's necessary that for each symbol x, the number of occurrences of x is at least something? Actually, consider p=1: need each symbol appears at least once. For p=2: need for each ordered pair (x,y), there is an occurrence of x before an occurrence of y. This implies that for each x, the set of symbols that appear after some x must be all symbols. In other words, after the last occurrence of x, we cannot have any symbol y that never appears after x. But more precisely, we need that for each x and y, there exist i<j with a_i=x, a_j=y. So this condition is equivalent to: For each x, the subsequence consisting of all occurrences after the first occurrence of x (or more exactly after some occurrence) must contain all symbols? Actually, if we fix x, we need that for every y, there is some occurrence of x that is followed by a later occurrence of y. This means that the set of symbols that appear after the last occurrence of x must be empty? Let's analyze: Suppose we have the positions of x: i1 < i2 < ... < ik. For a given y, we need some i_t and a later j with a_j=y. If the last occurrence of x is at i_k, then after i_k there must be y for all y. So after the last occurrence of each symbol x, the remaining tail must contain all symbols. That's a necessary condition? But is it sufficient for p=2? Actually, if after the last occurrence of x we have all symbols, then we can pair that last x with each y. But what if there is no y after last x, but there is some earlier x with y after? That would also work. So the condition for p=2 is: For each ordered pair (x,y), there exists an occurrence of x before an occurrence of y. This is equivalent to: There is no pair (x,y) such that every occurrence of x is after the last occurrence of y (i.e., no x appears before any y). In other words, the set of positions of x must not be entirely after the last occurrence of y. So more concisely, the sequence must have the property that the relative order of symbols is "rich".
For general p, there is known concept: The density p is exactly the minimum over all symbols of something? Or it's related to the length of the longest "alternating" pattern? Let's test small c.
Consider c=2. The condition to contain all binary words of length p. For a binary sequence, to contain all 2^p words of length p, what is needed? This is known: The sequence must be a "superpattern" for subsequences, sometimes called "universal sequence". The minimal length needed to contain all binary words of length p as subsequences is p+ (p choose something)? Actually, the minimal length for a sequence over alphabet of size c to contain all words of length p as subsequences is known: It is something like the smallest n such that there exists a sequence of length n that is "p-universal". There is known concept of "De Bruijn sequence" for substrings, but for subsequences, it's different. I recall that the minimum length needed is something like p + (p-1)*c? Not sure.
But we need to compute for each subsequence of given original sequence its density. This suggests we need to compute, for each subset of indices, some property. That is combinatorial explosion, so we need to aggregate.
Maybe the density of a sequence can be expressed in terms of the lengths of "runs" or "gaps"? Let's think about a characterization: A sequence S contains all words of length p if and only if for every way to partition the alphabet into two sets? Hmm.
Alternatively, think about the concept of "subsequence rank" or "VC-dimension"? Not exactly.
Another angle: Given sequence S, consider the set of all subsequences it can produce. The density p is the largest integer such that the set of subsequences of length p equals all c^p possibilities. That is like S is "p-complete". This condition is equivalent to: For any string w of length p, there is an embedding mapping positions of w to increasing indices in S. This is reminiscent of property of "containing all permutations" which is known to be related to "pattern avoidance". For permutations, a sequence of numbers (distinct) contains all permutations of length p if and only if it has "width" at least p? Not exactly.
But maybe we can think of it in terms of "color frequencies" and "order". For each prefix of S, we can compute the set of words that can be formed. Hard.
Given constraints n,c <= 3000, perhaps we can compute s_p via DP over the original array that builds subsequences and tracks some measure that determines the maximum p. Perhaps the density p of a subsequence T can be expressed as the minimum over all symbols of something like: For each symbol x, let f_x be the number of times x appears? Actually, for containing all words of length p, a necessary condition is that each symbol appears at least p? Not necessarily: Consider c=2, p=2. If we have sequence 1,2,1,2. Contains all pairs? Check: 1,1: indices 1,3 => yes; 1,2: 1,2 or 1,4 or 3,4 => yes; 2,1: 2,3 => yes; 2,2: 2,4 => yes. So contains all pairs. Counts: 1 appears twice, 2 appears twice. So each appears at least 2? Actually p=2, each appears at least 2? If we had 1 appears once, 2 appears many, could we have pair (1,1)? No, because need two 1's. So for p=2, each symbol must appear at least 2? For c=2, yes. For general p, to have (x,x,...,x) all same symbol of length p, we need at least p occurrences of x. So necessary condition: For each symbol x, its count in T, cnt_x >= p. So density p cannot exceed min_x cnt_x. Because if some symbol appears less than p, then the word consisting of that symbol repeated p times cannot be a subsequence. So p <= min_x cnt_x.
But is that also sufficient? Not at all. For example, c=2, counts: two 1's and two 2's as above gave p=2. But consider sequence: 1,1,2,2. Check pairs: 1,2 exists? indices 2,3 => yes. 2,1? Need a 2 before a 1. The 2's are after the 1's, so we have no 2 before 1. So pair (2,1) is missing. So even though min count=2, density is 1. So we need additional structure.
So p <= min cnt_x is necessary but not sufficient.
What else? For p=2, we need for each ordered pair (x,y), there exist occurrences i<j with a_i=x, a_j=y. This is equivalent to: For each x, the set of symbols that appear after some occurrence of x must be all symbols. A more succinct condition: The sequence, when you look at the order of first occurrences and last occurrences, something like: For each symbol x, the last occurrence of x must be before the first occurrence of some other symbol? Actually, to have (y,x) for y!=x, we need a y before an x. That means for each x, there must be some y (maybe same) that appears before the first occurrence of x? No, we need for all y, there exists y before x. So for each x, every other symbol y must appear before at least one occurrence of x. That is: For each x, the first occurrence of x must be after at least one occurrence of each y? Actually, to have (y,x) we need some y before some x. It could be that the first x occurs after some y, but if that y appears only before the first x and not before later x, that's fine. But if some y appears only after the last x, then (y,x) would not be possible because any y occurs after all x, so cannot have y before x. So for each x, there must be at least one occurrence of each y that occurs before the last occurrence of x? More precisely, for (y,x) to exist, we need an occurrence of y that is before an occurrence of x. So if all occurrences of y are after the last occurrence of x, then (y,x) is impossible. Thus, for each ordered pair (y,x), we need that the last occurrence of x is after the first occurrence of y? Not exactly: We need some y before some x. That is equivalent to: The last occurrence of x is after the first occurrence of y? Actually, if the last occurrence of x is after the first occurrence of y, then we can pick that first y and that last x to get y before x. But is that sufficient? Yes: If first_y < last_x, then there exists an index of y (first_y) and an index of x (last_x) such that first_y < last_x, so (y,x) is a subsequence. Conversely, if there exists some y before some x, then first_y < last_x holds? Not exactly: If there exists y_i < x_j, then first_y <= y_i < x_j <= last_x, so first_y < last_x. So condition for (y,x) existence is first_y < last_x. So for p=2, necessary and sufficient condition: For all ordered pairs (x,y) (we consider (x,y) meaning x then y), we need first_x < last_y. Wait careful: For pair (x,y) we need an occurrence of x before an occurrence of y. That is equivalent to first_x < last_y? As argued: If first_x < last_y, then choose the first occurrence of x and the last occurrence of y, we get x before y. Conversely, if there exist x_i < y_j, then first_x <= x_i < y_j <= last_y, so first_x < last_y. So yes, condition is first_x < last_y. So for all ordered pairs (x,y), we need first_x < last_y.
Thus for p=2, the condition is: For all symbols x,y, first_x < last_y. That's equivalent to: max_{x} first_x < min_{y} last_y? Actually, we need for all pairs, first_x < last_y. This is equivalent to: max over x of first_x < min over y of last_y. Because if max_first < min_last, then certainly for any x, first_x <= max_first < min_last <= last_y for any y, so first_x < last_y for all x,y. Conversely, if there exists x,y with first_x >= last_y, then max_first >= first_x >= last_y >= min_last, so max_first >= min_last. So condition is exactly: max(first occurrences) < min(last occurrences). So the density >=2 is equivalent to: The maximum first occurrence index among symbols is less than the minimum last occurrence index among symbols. Additionally, we need each symbol appears at least 2? Actually, does the condition first_x < last_y for all x,y imply each symbol appears at least twice? Consider x=y: For (x,x) we need two occurrences of x. The condition first_x < last_x would require that there is at least one later occurrence after the first, i.e., at least two occurrences. So indeed for all x, we need first_x < last_x, meaning at least two occurrences. So the condition for p>=2 is exactly: max_first < min_last and also each symbol appears at least 2? Actually max_first < min_last implies for any x, first_x <= max_first < min_last <= last_x? Wait, does min_last <= last_x for all x? Yes, min_last is the minimum of last_y over y, so it is <= last_x for any specific x. So we have first_x <= max_first < min_last <= last_x. Thus first_x < last_x for each x, so each symbol appears at least twice. So the condition reduces to just max_first < min_last. So for p=2, it's sufficient that the earliest last occurrence among symbols is after the latest first occurrence. That's a neat condition.
Now consider p=3. We need that for any triple (x,y,z), there exist indices i<j<k with a_i=x, a_j=y, a_k=z. Is there a similar characterization? Perhaps we can generalize: Define for each symbol x, the positions of its occurrences. The condition for all triples is more complex. But maybe we can think in terms of "interleavings". There is known result: The maximum p such that a sequence contains all words of length p is equal to the minimum over all symbols of something like the number of times that symbol appears? Or maybe it's equal to the minimum over all pairs of something? Let's try to derive.
We want to embed any word w of length p. This is similar to the concept of "sequence is p-universal" iff it has as a subsequence every word of length p. There is a known characterization using "separators" or "cuts". For a given sequence S, define its "profile" as the length of the longest word that is NOT a subsequence (or the shortest missing word). That's like the "subsequence rank".
Consider the following: For each position i, define the set of words that can be formed as subsequences ending at i? That's too many.
Another approach: There is a known concept: "Shannon capacity" but not.
Maybe we can think in terms of "graph" of occurrences. For each symbol x, we have a list of positions. To embed a word w, we need to choose positions from these lists in increasing order. This is like a matching problem. The condition that all words of length p are subsequences is equivalent to: For any sequence of symbols w1..wp, the concatenated condition that there exists an increasing selection from the occurrence lists. This is reminiscent of the condition that the poset formed by the occurrence lists is "p-regular" or "p-superimposed".
Perhaps we can rephrase: For each p, the property can be described in terms of the "shuffle" operation. There is known result: A sequence S contains all words of length p iff for every way to partition S into p+1 parts (by cutting at positions), something about the alphabet coverage? Not sure.
Let's try to derive necessary conditions for p=3. For all triples (x,y,z). In particular, for any x, we need (x,x,x) so we need at least 3 occurrences of each symbol: cnt_x >=3. Also, for any x,y, we need (x,x,y), (x,y,x), (y,x,x) etc. Many conditions.
Consider condition for (x,y,z) with x,y,z distinct. That requires that there exists an occurrence of x before some occurrence of y before some occurrence of z. This is reminiscent of the "order type" of the sequence. For p=2, we derived condition: max_first < min_last. For p=3, maybe we need: max_first_of_2nd? Let's attempt to generalize.
Let’s define for each symbol x, let its occurrences be positions p_x^1 < p_x^2 < ... < p_x^{cnt_x}. For a given word w = (w1,w2,...,wp), we need to pick indices i1 < i2 < ... < ip such that a_{i_j}=w_j. This is possible iff for each j, we can select an occurrence of w_j that is after the previous selection.
A necessary and sufficient condition for all words of length p might be something like: For any way to split the alphabet into two subsets? Hmm.
Maybe we can think in terms of "intervals". Consider the following: For each symbol x, define the interval [first_x, last_x]. For p=2, condition max_first < min_last ensures that for any two symbols x,y, there is an x before a y (since first_x < last_y). But what about requiring x before y before z for p=3? Intuitively, we need that after picking x, we can pick y and then z. One might think that condition max_first < min_mid < max_mid < min_last? Something like that.
Let’s try to define the concept of "pth extremal positions". Possibly we can define for each symbol x and each k from 1 to p, the k-th occurrence (or the position of the k-th occurrence). Then the condition for universal p is: For any sequence of symbols w1..wp, there exist indices i1<...<ip such that i_j is at least the something? More concretely, for a given word, we can greedily match: to embed w, we go through w, and for each symbol, we pick the earliest occurrence after the previous position. This greedy algorithm yields a match if and only if the word is a subsequence. So the condition that all words are subsequences is equivalent to: For any word w, the greedy algorithm succeeds. This greedy algorithm fails only if at some step, for symbol w_j, there is no occurrence after the current position.
Thus, to guarantee that all words of length p are subsequences, we need that no matter what choices we make (the current position), the remaining tail of the sequence contains all symbols sufficiently many times? This is akin to the property that after any prefix of the sequence, the remaining suffix still contains all symbols enough times? For p=2, we had that after the last first occurrence (i.e., after position max_first), the suffix must contain all symbols (since we need to match second symbol of any pair). Actually, to match any second symbol after we've chosen first symbol, we need that after the chosen first occurrence, there is an occurrence of any possible second symbol. Since we can choose first occurrence greedily as the earliest, but we might need to consider worst-case first symbol? Actually, to guarantee that any pair (x,y) is a subsequence, we need that for any x, after some occurrence of x (in particular after the first occurrence of x) there is an occurrence of y. But is it enough that after the first occurrence of each x, there is a y? If after first_x there is a y for all y, then (x,y) works: use first_x and then the y after it. However, if after first_x there is no y, but there is some later x that has y after it, is (x,y) still possible? Yes, we could use a later x that appears before a y. But if there is no occurrence of x before any y at all, then (x,y) fails. This condition is exactly first_x < last_y. That uses the first occurrence of x and the last occurrence of y. So the condition uses extremal positions.
For p=3, we might need to consider the first occurrence, second occurrence, and last occurrence etc. Maybe we need for each symbol x, we need at least p occurrences, and the ordering of these occurrences across symbols must be "interleaved" in a specific way.
I recall a known result: The maximum p such that a sequence contains all words of length p as subsequences is equal to the minimum over all symbols of the number of times that symbol appears? But the counterexample earlier: sequence 1,1,2,2 has min count=2 but p=1, not 2. So that's not enough.
What about the "alternating" condition? Sequence 1,2,1,2 works. Sequence 1,1,2,2 fails because the symbols are grouped. So the interleaving matters.
Maybe the density p is determined by the size of the largest "alternating" subsequence? Or the "minimum over all symbols of the length of the longest alternating subsequence"? Not sure.
Let's try to think about building a DP that, given the original array, counts subsequences by their density. Since n and c are up to 3000, perhaps we can compute the density for each subsequence using some combinatorial invariant that is additive.
Another idea: The density p of a subsequence T is equal to the length of the longest common subsequence between T and some "universal" sequence? That seems not.
Maybe we can reframe: For a given subsequence T, its density is the largest p such that for every word w of length p, w is a subsequence of T. Equivalent to: There is no word of length p+1 that is not a subsequence? Actually, density is maximal p such that all words of length p are subsequences. So p is the "universality index".
Now, there is known concept: The "subword (or subsequence) universality" has been studied. A sequence is called p-universal if it contains all possible words of length p as subsequences. There are known characterizations in terms of "arch factorizations" or "Richness". I recall a property: A sequence over alphabet Σ is p-universal iff for every factorization into p+1 factors (i.e., split the sequence into p+1 contiguous blocks), there exists a block that contains all symbols? Not exactly.
Let's try to derive a structural characterization. Suppose we have a sequence T. Consider the following: For each k from 1 up to p, we can define the "k-th occurrence frontier". Define f_k(x) = position of the k-th occurrence of symbol x (or ∞ if less than k occurrences). Then, for T to be p-universal, we need that for any sequence of symbols w1...wp, there exist indices i1<...<ip with a_{i_j}=w_j. This is equivalent to: For any sequence of symbols w1..wp, the greedy algorithm that picks the earliest possible occurrence for each w_j succeeds. The greedy algorithm will succeed if and only if for each j, after picking the (j-1)-th occurrence, there remains an occurrence of w_j. So we need that no matter what the previous picks (which are themselves symbols), the remaining tail contains an occurrence of the needed symbol. This is like a "Hall's marriage" condition for matching positions to pattern positions.
Perhaps we can think of it as: For any set of symbols (with multiplicities) the positions of occurrences must be "rich" enough.
There is a known theorem: A sequence contains all words of length p iff it has at least p occurrences of each symbol, and the ordering of the p-th occurrences of symbols is "interleaved" such that the sequence of the first occurrences, second occurrences, ..., p-th occurrences are all interleaved? For p=2, we had condition max(first occurrences) < min(last occurrences). That can be restated as: The sequence of first occurrences (ordered by position) and the sequence of second occurrences (i.e., the last occurrences if only two occurrences) are such that the maximum of the firsts is less than the minimum of the seconds. For p=3, perhaps we need: Let positions of first occurrences be a_1,...,a_c (for each symbol). Let positions of second occurrences be b_1,...,b_c. Let positions of third (or last) occurrences be c_1,...,c_c. Then condition might be: max(a_i) < min(b_i) and max(b_i) < min(c_i). Let's test on sequence that is 3-universal, e.g., with c=2, p=3. What is an example of a binary sequence that contains all 8 words of length 3? Let's try to construct: need all triples: 111,112,121,122,211,212,221,222. We need at least three 1's and three 2's. Also need ordering constraints. Consider sequence: 1,2,1,2,1,2. That's alternating 1,2 repeated. Check if all triples exist? 111? Need three 1's: positions 1,3,5 => yes. 222? positions 2,4,6 => yes. 112? indices 1 (1),3 (1)? Actually need 1,1,2: we can take positions 1,3,4 (1,1,2) => yes. 121? 1,2,1: 1,2,3 => yes. 122? 1,2,4? 1,2,4 gives 1,2,2 -> yes. 211? 2,1,3? 2,1,3 gives 2,1,1? Actually 2,1,1 not 211. Need 2,1,1: positions 2,3,5? 2,3,5 => 2,1,1? That's 2,1,1 not 2,1,1? That's fine. Actually 211 is 2,1,1, yes. So 2,3,5 works. 212? 2,1,4? 2,1,4 => 2,1,2 yes. 221? 2,4,5? 2,4,5 => 2,2,1? That's 2,2,1 -> yes. So alternating sequence of length 6 works for p=3? Let's test 221: positions 2,4,5 gives 2,2,1, okay. So 1,2,1,2,1,2 seems 3-universal? But does it contain 122? 1,2,4 gives 1,2,2, okay. So yes. Now check our proposed condition: For c=2, first occurrences: 1 at pos1, 2 at pos2 => max_first = 2. Second occurrences: 1 at pos3, 2 at pos4 => max_second = 4, min_second = 3. Third occurrences: 1 at pos5, 2 at pos6 => max_third = 6, min_third = 5. Check: max_first=2 < min_second=3? Yes. max_second=4 < min_third=5? Yes. So condition holds.
Now test a sequence that should fail p=3. For example, 1,1,1,2,2,2. This has three 1's then three 2's. Check if it is 3-universal? Need 2,1,1? That would require a 2 before a 1, but all 2's are after all 1's, so 2,1,1 impossible. So p<=2. Indeed, min count is 3, but p=2? Check p=2: Does it contain all pairs? Need 2 before 1? No, because all 2's after 1's, so (2,1) missing. So p=1? Actually (1,1), (1,2), (2,2) exist, but (2,1) missing, so p=1. So density=1. Now compute our condition: first occurrences: 1 at 1, 2 at 4 => max_first=4. second occurrences: 1 at 2, 2 at 5 => max_second=5, min_second=2? Actually min_second is min of second occurrences = min(2,5)=2. Check max_first=4 < min_second=2? 4<2 false. So fails. And for p=2 we would need max_first < min_second? According to our hypothesis for p=2 we had max_first < min_last (i.e., min of last occurrences). For this sequence, last occurrences: 1 at 3, 2 at 6 => min_last = 3. max_first=4 < 3 false. So fails.
What about p=3 condition would be max_first < min_second and max_second < min_third? Here third occurrences: 1 at 3? Actually third occurrence of 1 is at 3, third of 2 at 6 => min_third=3. max_second=5 < 3 false. So fails.
Now test sequence 1,2,2,1,2,1 (c=2). Let's compute occurrences: 1: positions 1,4,6; 2: positions 2,3,5. Does it contain all triples? Check 212? 2,1,2: can we? positions 2,4,5 -> 2,1,2 yes. 221? 2,2,1: 2,3,4 -> 2,2,1? Actually 4 is 1, so 2,2,1 => yes. 122? 1,2,2: 1,2,3 -> 1,2,2 yes. 211? 2,1,1: 2,4,6 -> 2,1,1 yes. 111? 1,1,1: need three 1's: 1,4,6 works. 222? 2,2,2: 2,3,5 works. 121? 1,2,1: 1,2,4 works. 112? 1,1,2: 1,4,5? 1,4,5 gives 1,1,2? Actually 5 is 2, so 1,4,5 gives 1,1,2 yes. So it seems 3-universal. Check our condition: first: 1 at 1, 2 at 2 => max_first=2. second: 1 at 4, 2 at 3 => min_second = min(4,3)=3, max_second = max(4,3)=4. third: 1 at 6, 2 at 5 => min_third = min(6,5)=5, max_third = 6. Conditions: max_first=2 < min_second=3 true; max_second=4 < min_third=5 true. So holds.
Now test a borderline case: Sequence 1,2,1,1,2,2. Positions: 1:1,3,4; 2:2,5,6. Check all triples? Need 2,1,1? 2,1,1: can we? 2 at 2, then 1 after: we have 3 and 4, so 2,3,4 gives 2,1,1 -> yes. 1,2,2? 1,2,5 => 1,2,2 yes. 2,2,1? 2,5,? after 5 we need 1, but last 1 is at 4, which is before 5, so not possible. Check if there is 2,2,1: need two 2's then a 1 after. The 2's are at 2,5,6; after the second 2 (say at 5) the next positions are 6 (2) and then end; no 1 after 5. Could we use 2 at 2 and 2 at 6? That gives indices 2 and 6, but after 6 there is no 1. So 2,2,1 missing. So not 3-universal. Check condition: first: 1 at1,2 at2 => max_first=2. second: 1 at3,2 at5 => min_second=3, max_second=5. third: 1 at4,2 at6 => min_third=4, max_third=6. Check max_second=5 < min_third=4? 5<4 false. So fails. So condition holds.
Thus for these examples, the condition max of k-th occurrence < min of (k+1)-th occurrence for all k from 1 to p? Actually our condition for p=3 used first, second, third occurrences. Specifically, we required: max(1st) < min(2nd) and max(2nd) < min(3rd). For p=2, condition is max(1st) < min(2nd) (since second occurrences are also last if only two needed? But for p=2 we used max_first < min_last, which is max(1st) < min(2nd). So pattern emerges: For a sequence to be p-universal, it might be necessary and sufficient that for each k = 1, 2, ..., p, the maximum of the positions of the k-th occurrence among all symbols is less than the minimum of the positions of the (k+1)-th occurrence among all symbols. However, note that for p, we only care about words of length p. The condition would involve occurrences up to p-th. But do we need (p+1)-th? Possibly we need for each k from 1 to p-1, max(k-th) < min((k+1)-th). Also we need each symbol appears at least p times (to cover words with all same symbol). This condition also ensures that.
Let's test with p=1. Condition would be: max(0-th?) Actually for p=1, we need each symbol appears at least once (so that all singletons exist). There is no need for ordering condition between occurrences. The condition max(1st) < min(2nd) would be for p>=2 only. For p=1, condition is simply each symbol appears at least once. That can be expressed as "min(1st) exists" but not about max and min of 1st vs 2nd. So the pattern might be: For p>=1, density >= p iff (i) each symbol appears at least p times, and (ii) for all k from 1 to p-1, max_{x} (position of k-th occurrence of x) < min_{x} (position of (k+1)-th occurrence of x). Let's denote occ[x][k] = position of the k-th occurrence of symbol x in the sequence (or INF if not enough). Then condition (ii) says: For each k = 1..p-1, max_{x} occ[x][k] < min_{x} occ[x][k+1].
We should verify if this condition is indeed necessary and sufficient for p-universality. Let's try to prove.
First, necessity: Suppose the sequence T contains all words of length p. Then for each symbol x, the word consisting of p copies of x must be a subsequence, so x appears at least p times. So (i) holds.
Now, consider any k from 1 to p-1. For any two symbols x and y, consider the word consisting of k copies of x followed by (p-k) copies of y (i.e., x...x y...y). This word must be a subsequence. This means there exist indices i1 < ... < ik < j1 < ... < j_{p-k} such that the first k are x and the next p-k are y. In particular, there must be at least k occurrences of x, and at least p-k occurrences of y, but we already have that. Moreover, we can choose the k-th occurrence of x and the first occurrence of y after that? Actually to embed, we need some occurrence of x that is the k-th used x, but we want to ensure that the position of the k-th used x is before the position of the (p-k)-th used y? Not exactly. However, we can derive a condition on the occurrence positions. Consider the specific word: k copies of x followed by (p-k) copies of y. Since this must be a subsequence, there must exist indices for the x's and y's. Let i_k be the position of the last x used (the k-th x), and let j_1 be the position of the first y used after that. Then we have i_k < j_1. Since we can choose any occurrences, we can potentially choose the earliest possible occurrences for x and the latest for y? But to derive a condition that relates the actual k-th occurrence of x (i.e., the k-th occurrence in the whole sequence) and the (p-k)-th occurrence of y? Wait, we need to guarantee existence for all x,y. We want to prove that the k-th occurrence of any symbol must occur before the (k+1)-th occurrence of any other symbol? Actually the condition we proposed is: max over symbols of k-th occurrence < min over symbols of (k+1)-th occurrence. Let's try to see if necessary.
Take any two symbols u and v. Consider the word consisting of k copies of u followed by (p-k) copies of v. Since this word must be a subsequence, there must exist positions for the u's and v's. Let the positions chosen for u's be some increasing positions, and for v's be after the last u. In particular, we need at least k occurrences of u, and at least p-k occurrences of v after the last chosen u. This implies that the k-th occurrence of u (which is the earliest possible position for the last u if we choose the first k u's) might not be the one used, because we could choose later u's. But we can always choose the earliest possible set of k u's? To minimize the position of the last u, we can pick the first k occurrences of u. So if the word is a subsequence, then there exists some selection of u's and v's. But we can also argue that if the word is a subsequence, then in particular, the k-th occurrence of u must occur before the (p-k+1)-th occurrence of v? Not necessarily. Because we could use later u's (beyond the k-th) and earlier v's? Let's try to find a counterexample where condition fails but still all words of length p exist.
Suppose we have c=2, p=2. Condition: max(1st) < min(2nd). This is necessary as we argued earlier for p=2. Indeed we proved necessity via (x,y) pairs requiring first_x < last_y. That's equivalent to max first < min last. So condition holds for p=2.
Now for p=3. Suppose the condition fails: e.g., max(2nd) >= min(3rd). So there exist symbols u and v such that the 2nd occurrence of u is at position >= the 3rd occurrence of v. That is, occ[u][2] >= occ[v][3]. Does that preclude some word of length 3? Consider the word: u, u, v? That would be two u's then a v. To embed u,u,v, we need two u's before a v. The best we can do is take the first two u's (positions occ[u][1] and occ[u][2]) and then a v after occ[u][2]. But if occ[u][2] >= occ[v][3], then after occ[u][2], there may still be v's, but we need at least one v after occ[u][2]. Since occ[v][3] is the third occurrence of v, there are at most two v's before occ[v][3]; after occ[u][2], if occ[u][2] >= occ[v][3], then all v's (including the third) occur at or before occ[u][2]. Actually if occ[v][3] <= occ[u][2], then any v occurrence after occ[u][2] would be after occ[v][3], but there may be more v's after occ[v][3]? The third occurrence is not necessarily the last; there could be a fourth occurrence. However, the condition only says the third occurrence is before occ[u][2]; there might be a fourth occurrence after. So the existence of a word u,u,v is not necessarily prevented. But consider the word v,v,u? That would be two v's then a u. If occ[u][2] >= occ[v][3], we have that the third v occurs at or before the second u. Then to embed v,v,u, we need two v's before a u. The first two v's are occ[v][1], occ[v][2]. Their positions are <= occ[v][3] (since occ[v][1] < occ[v][2] < occ[v][3] <= occ[u][2]). So the second v is at occ[v][2] <= occ[u][2]. But we need a u after the second v. The second u occurs at occ[u][2] (or later). Since occ[v][2] <= occ[u][2], we can take u at occ[u][2] (or later) which is after occ[v][2]? But we need u after the second v. If occ[v][2] <= occ[u][2], then occ[u][2] is after occ[v][2]? Possibly occ[u][2] could be equal to occ[v][2]? But indices are distinct positions; they can't be equal. So occ[v][2] < occ[u][2]? It could be that occ[v][2] < occ[u][2] if occ[v][3] <= occ[u][2] and occ[v][2] < occ[v][3] <= occ[u][2]. Then occ[v][2] < occ[u][2], so we can pick u at occ[u][2] after the second v. So v,v,u might be possible.
What about the word: u, v, v? That requires one u before two v's. Since we have many v's, maybe possible. So failure condition might not directly kill a specific word.
But maybe there is a known theorem: The sequence is p-universal iff the list of occurrences is "p-interleaved". I recall a result: For a sequence to contain all words of length p, the occurrences of symbols must be such that when you list the positions of first occurrences, second occurrences, etc., they are in increasing order across the alphabet? Actually, consider the matrix where rows are symbols, columns are occurrence indices. The condition that all words of length p are subsequences is equivalent to the condition that for each k from 1 to p, the k-th column (i.e., the k-th occurrence of any symbol) has all its entries less than the (k+1)-th column entries. More precisely, if we sort the positions of the k-th occurrences across symbols, we need that the maximum k-th occurrence is less than the minimum (k+1)-th occurrence. Because then we can greedily match any word: For word w1..wp, we can pick the first occurrence of w1, then the second occurrence of w2 after that, etc., using the fact that the (j)-th occurrence of w_j will be after the (j-1)-th occurrence of previous symbols? Actually, we need to prove sufficiency: If for all k=1..p-1, max occ[x][k] < min occ[y][k+1] (over all x,y), then the sequence is p-universal. Let's test with an example that might violate but still p-universal? Suppose c=2, p=3, sequence: 1,2,1,2,1,2 we already saw works and satisfies. Consider a sequence that still works but maybe violates the condition? Let's try to construct a sequence that is 3-universal but fails max(2nd) < min(3rd). For c=2, need each symbol appears at least 3 times. Suppose we have positions: 1:1, 2:1, 3:2, 4:2, 5:1, 6:2. That is 1,1,2,2,1,2. Let's check if all triples exist? We already earlier considered 1,1,2,2,1,2 (the same as 1,2,2,1,2,1? Actually that's 1,1,2,2,1,2). Let's test: 1,1,2? need two 1's then a 2 after. Positions: 1,2 are 1,1; after 2 we have 3 is 2, so works. 1,2,1? 1,3,5? 1,3,5 gives 1,2,1 works. 1,2,2? 1,3,4 works. 2,1,1? 3,5,? after 5 we have 6 is 2, not 1; but could use 2 at 4? 4,5,? 4 is 2,5 is 1, need another 1 after 5? there is no 1 after 5 (only 6 is 2). So 2,1,1? We need 2,1,1. Could use 2 at 3, then 1 at 5, then need another 1 after 5: none. 2 at 4, then 1 at 5, then need 1 after 5: none. So 2,1,1 missing. So not 3-universal. Let's compute occurrences: 1: positions 1,2,5; 2: positions 3,4,6. occ[1][1]=1, occ[1][2]=2, occ[1][3]=5; occ[2][1]=3, occ[2][2]=4, occ[2][3]=6. max(1st)=max(1,3)=3; min(2nd)=min(2,4)=2; 3<2? false. Condition fails already at k=1. So that's consistent.
Try to find a potential counterexample: We need each symbol appears at least 3 times. Suppose we arrange such that max(2nd) >= min(3rd). For example, let occ[1][2] = 5, occ[2][3] = 4. Then max(2nd)=5, min(3rd)=4, condition fails. Can we still have all triples? Let's try to construct: c=2. Let positions: 1:1, 2:2, 3:1, 4:2, 5:1, 6:2? That's alternating, which satisfies condition. To violate, we need something like: 1 appears at positions 1,4,5; 2 appears at 2,3,6. Then occ[1][1]=1, occ[1][2]=4, occ[1][3]=5; occ[2][1]=2, occ[2][2]=3, occ[2][3]=6. Then max(1st)=max(1,2)=2; min(2nd)=min(4,3)=3 => 2<3 ok. max(2nd)=max(4,3)=4; min(3rd)=min(5,6)=5 => 4<5 ok. So condition holds.
To violate, we need occ[2][3] to be less than occ[1][2]. That is, the third occurrence of 2 comes before the second occurrence of 1. Example: positions: 1:1 (occ1_1), 2:2 (occ2_1), 3:2 (occ2_2), 4:2 (occ2_3), 5:1 (occ1_2), 6:1 (occ1_3). Then occ1: 1,5,6; occ2:2,3,4. Then max(1st)=max(1,2)=2; min(2nd)=min(5,3)=3 => 2<3 ok. max(2nd)=max(5,3)=5; min(3rd)=min(6,4)=4 => 5<4 false. So condition fails at k=2. Now test universality for p=3. Sequence: 1,2,2,2,1,1. Check all 8 triples:
- 111: need three 1's at positions 1,5,6 -> yes.
- 112: 1,1,2: positions 1,5,? after 5 we need a 2: positions after 5 are 6 (1) only, no 2. Could use 1,6,? no 2 after 6. Could use 1,5,? no. Could use 5,6,? no. So 112 missing? Check if 1,1,2 can be formed with 1's at 1,5 and 2 after 5: there is no 2 after 5 because 2's are at 2,3,4 all before 5. So 112 missing. So not universal. So condition seems necessary.
Now consider a case where condition holds but maybe universality fails? Let's try to find a counterexample for p=3 where max(1st) < min(2nd) and max(2nd) < min(3rd) but some triple missing. Suppose c=3, p=2 maybe? For p=2, condition is max(1st) < min(2nd). Is that sufficient? We earlier argued that for p=2, it's also sufficient: if max first < min second, then for any pair (x,y), we have first_x <= max_first < min_last <= last_y, so first_x < last_y, so pair exists. Also for (x,x) we need two occurrences: first_x < last_x (since min_last <= last_x, and max_first >= first_x, but we have first_x <= max_first < min_last <= last_x, so first_x < last_x). So indeed condition is sufficient for p=2.
Now for p=3, we need to verify sufficiency: If each symbol appears at least 3 times and max(1st) < min(2nd) and max(2nd) < min(3rd), does it guarantee that all triples are subsequences? Let's attempt to prove.
Given any word w = (a,b,c). We need to find indices i<j<k with those symbols. We have positions: occ[a][1], occ[a][2], occ[a][3] (maybe more). Similarly for b and c. Condition: max(1st) < min(2nd) and max(2nd) < min(3rd). This implies that the positions of first occurrences of all symbols are before all second occurrences of any symbols, and all second occurrences are before all third occurrences. In other words, if we list all positions of first occurrences (one per symbol) they all come before any second occurrence, and all second occurrences come before any third occurrence. Additionally, there may be additional occurrences beyond the third, but they would be after third occurrences.
Now, to embed a triple, we can try to use the first occurrence for the first symbol, second occurrence for the second symbol, third occurrence for the third symbol? But careful: The second symbol might be the same as the first, etc. For example, if a=b, then we need two occurrences of a for the first two positions. Since a appears at least 3 times, we have occ[a][1], occ[a][2], occ[a][3]. Since occ[a][1] is a first occurrence, occ[a][2] is a second occurrence. Because of the condition, occ[a][1] < min(2nd) <= occ[a][2] (since occ[a][2] is among second occurrences, min(2nd) <= occ[a][2]), and we have max(1st) < min(2nd), so occ[a][1] <= max(1st) < min(2nd) <= occ[a][2], so occ[a][1] < occ[a][2]. Also occ[a][2] < min(3rd) <= occ[a][3], so occ[a][2] < occ[a][3]. So the first three occurrences of each symbol are strictly increasing and separated globally: all first occurrences < all second occurrences < all third occurrences.
Now for any triple (a,b,c), we can pick:
-
If a,b,c are all distinct: pick occ[a][1] (first occurrence), occ[b][2] (second occurrence), occ[c][3] (third occurrence). Since occ[a][1] < min(2nd) <= occ[b][2] and occ[b][2] <= max(2nd) < min(3rd) <= occ[c][3], we have occ[a][1] < occ[b][2] < occ[c][3]. So triple exists.
-
If a=b≠c: we need two a's then c. We can pick occ[a][1], occ[a][2], occ[c][3]. Because occ[a][1] < occ[a][2] (since they are first and second) and occ[a][2] < min(3rd) <= occ[c][3], so works.
-
If a≠b, b=c: need a, b, b. Pick occ[a][1], occ[b][2], occ[b][3]. Since occ[a][1] < min(2nd) <= occ[b][2] and occ[b][2] < occ[b][3] (as second<third) and occ[b][2] < min(3rd) <= occ[b][3] ensures occ[b][2] < occ[b][3]; also occ[a][1] < occ[b][2] is guaranteed. So works.
-
If a=c≠b: need a, b, a. Pick occ[a][1], occ[b][2], occ[a][3]. Need occ[a][1] < occ[b][2] < occ[a][3]. We have occ[a][1] < min(2nd) <= occ[b][2]; also occ[b][2] <= max(2nd) < min(3rd) <= occ[a][3]; so holds.
-
If a=b=c: need three a's. Pick occ[a][1], occ[a][2], occ[a][3]; works.
Thus sufficiency holds.
So for p=3, condition (i) each symbol appears at least p times, and (ii) for k=1..p-1, max(occ[x][k]) < min(occ[x][k+1]) is sufficient.
Now, is it necessary for general p? Let's try to generalize. Suppose the sequence is p-universal. Then for any symbols x and y, consider the word consisting of k copies of x followed by (p-k) copies of y. This must be a subsequence. The earliest possible position for the k-th x (if we try to use the earliest occurrences) is occ[x][k]. The latest possible position for the (p-k)-th y? Actually, to embed the word, we need some occurrence of the k-th x before some occurrence of the first y after that? Wait, we need to find indices i1<...<ik<j1<...<j_{p-k} with x's and y's. In particular, the k-th x must be before the first y after it. The earliest we can place the k-th x is occ[x][k]. To guarantee existence, there must be at least (p-k) occurrences of y after that position. The (p-k)-th occurrence of y after that position could be as early as occ[y][p-k] if occ[y][p-k] > occ[x][k]? Not exactly. But we can derive that occ[x][k] must be less than occ[y][p-k+1]? Because we need p-k y's after the k-th x. The (p-k)-th occurrence of y after the k-th x could be the (p-k)-th overall occurrence of y if occ[y][p-k] > occ[x][k]; but if occ[y][p-k] <= occ[x][k], then there are at most p-k-1 y's after occ[x][k]. However, we could use later x's, not necessarily the k-th occurrence. But if we consider the worst-case word, we might need to ensure that no matter which x and y, there exists some selection. The necessity of the condition max(occ[x][k]) < min(occ[x][k+1]) might be proven by considering specific words that mix symbols in a clever way.
Consider k from 1 to p-1. Let u be the symbol achieving the maximum position of its k-th occurrence, i.e., occ[u][k] = M = max_x occ[x][k]. Let v be the symbol achieving the minimum position of its (k+1)-th occurrence, i.e., occ[v][k+1] = m = min_x occ[x][k+1]. We need to show that if the sequence is p-universal, then M < m. Suppose for contradiction that M >= m. Then occ[u][k] >= occ[v][k+1]. Consider the word consisting of k copies of u followed by (p-k) copies of v. Can we embed it? For embedding, we need k u's and then p-k v's, with the k-th u before the first v after it. The earliest we can place the k-th u is at occ[u][k] (if we choose the first k u's). But if occ[u][k] >= occ[v][k+1], then note that there are at most k occurrences of v before or at occ[v][k+1]? Actually, the (k+1)-th occurrence of v is at position m. Since m <= M, we have that at position M (or earlier) there are at least k+1 v's? Let's analyze: occ[v][1] < occ[v][2] < ... < occ[v][k+1] = m <= M. So there are at least k+1 occurrences of v at positions <= M. Therefore, after position M, there are at most (cnt_v - (k+1)) occurrences of v. But we need p-k v's after the k-th u. Since p-k could be as large as p-k, and we don't know cnt_v. But from p-universality, we know cnt_v >= p. So cnt_v - (k+1) could be >= p - (k+1) = (p - k -1). So we need at least p-k v's after the k-th u, i.e., after position M. But we only guarantee that there are at most cnt_v - (k+1) v's after M, which is at least p-k-1? Actually, cnt_v >= p, so cnt_v - (k+1) >= p - k -1. So we could have exactly p-k-1 v's after M, which would be insufficient. But we could possibly choose a later u (beyond the k-th) to be the k-th u? Wait, when we need k u's, we don't have to use the first k occurrences; we could use later occurrences, which might be after the (k+1)-th v. However, if we choose later u's, then the position of the k-th u (the last u among those chosen) will be even larger, potentially reducing the number of v's after it further. The best chance is to minimize the position of the k-th u, i.e., use the earliest possible u's. So if even using the earliest k u's we cannot fit the required v's after, then there is no embedding. So we need to check if using the first k u's we can find p-k v's after occ[u][k]. Since occ[u][k] >= occ[v][k+1], there are at most cnt_v - (k+1) v's after occ[u][k] (because at least k+1 v's are at or before occ[u][k]). But cnt_v - (k+1) could be >= p-k? Not necessarily. For the worst-case, we can choose v such that cnt_v is exactly p (the minimal). So we need cnt_v - (k+1) >= p-k => cnt_v >= p+1? That's not guaranteed. Since cnt_v could be exactly p, then cnt_v - (k+1) = p - k -1, which is less than p-k. So we would be missing one v. Hence, embedding fails. But is it necessary that cnt_v = p? The universality condition forces each symbol to appear at least p, but could appear more. However, to disprove necessity, we need a counterexample where the condition fails but the sequence is still p-universal because some symbols have extra occurrences that compensate. Let's test: c=2, p=3. Suppose we have occ[u][2] >= occ[v][3] but cnt_v > p (say 4 occurrences). Then maybe the word (u,u,v) could be embedded using later u's? Let's try to construct such a sequence that is 3-universal but violates max(2nd) < min(3rd). We'll attempt to design with extra occurrences.
We need two symbols, say 1 and 2. We want occ[1][2] >= occ[2][3] (so max(2nd) = occ[1][2], min(3rd) = occ[2][3]), but still all 8 triples exist.
Let occ[1][2] = 5, occ[2][3] = 4. Then we need at least 3 occurrences of each, but 2 must have at least 3 occurrences, and we set its third at position 4. Then 2's occurrences: positions say 2,3,4? Actually third at 4 means first at 1? Let's try to assign positions such that occ[1][2]=5 > occ[2][3]=4. Also we need each symbol appears at least 3 times.
We need all triples: Let's try to construct. Let symbol 1 occurrences: maybe at positions 1,5,6,7 (so occ1_2=5, occ1_3=6). Symbol 2 occurrences: at positions 2