You have $$$n$$$ cards arranged in a row. The $$$i$$$-th card has the integer $$$a_i$$$ written on it. All integers are distinct.
You must color each card either $$$\color{red}{\text{red}}$$$ or $$$\color{blue}{\text{blue}}$$$ such that the following conditions are satisfied:
Determine if such a coloring exists.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 200$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the array.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$).
It is guaranteed that all elements of $$$a$$$ are distinct.
For each test case, output "YES" if you can color the cards so that the conditions are satisfied, and "NO" otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
442 3 4 132 3 153 4 1 2 553 1 4 2 5
YESNOYESNO
In the first example, the cards are colored as $$$a = [\color{red}{2}, \color{blue}{3}, \color{red}{4}, \color{blue}{1}]$$$. After sorting, the cards become $$$[\color{blue}{1}, \color{red}{2}, \color{blue}{3}, \color{red}{4}]$$$. Both sequences satisfy the condition, so the answer is YES.
In the second example, no valid coloring exists. For instance, if we color the cards as $$$a = [\color{blue}{2}, \color{red}{3}, \color{blue}{1}]$$$, the sorted sequence becomes $$$[\color{blue}{1}, \color{blue}{2}, \color{red}{3}]$$$. Here, the adjacent elements $$$1$$$ and $$$2$$$ have the same color, so the condition is not satisfied.
In the third example, a possible coloring is $$$a = [\color{blue}{3}, \color{red}{4}, \color{blue}{1}, \color{red}{2}, \color{blue}{5}]$$$. After sorting, the cards become $$$[\color{blue}{1}, \color{red}{2}, \color{blue}{3}, \color{red}{4}, \color{blue}{5}]$$$.
You are given an integer array $$$a$$$ consisting of $$$n$$$ elements. Denote $$$f(l, r) = \operatorname{MEX}([a_l, a_{l + 1}, \ldots, a_r])$$$$$$^{\text{∗}}$$$.
Determine if there is a way to reorder the array $$$a$$$ such that for every $$$i$$$ ($$$1 \le i \le n - 1$$$), $$$f(1, i) \neq f(i + 1, n)$$$. In other words, for every split point $$$i$$$, the $$$\operatorname{MEX}$$$ of the prefix must be different from the $$$\operatorname{MEX}$$$ of the suffix.
$$$^{\text{∗}}$$$The minimum excluded (MEX) of a collection of integers $$$c_1, c_2, \ldots, c_k$$$ is defined as the smallest non-negative integer $$$x$$$ which does not occur in the collection $$$c$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 100$$$) — the length of the array.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le n$$$).
Output "YES" if you can reorder $$$a$$$ so that the condition from the statement is satisfied, and "NO" otherwise. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
321 030 3 061 0 5 0 6 1
YESNOYES
In the first example, the initial ordering of $$$a$$$ already satisfies the condition. The only choice for $$$i$$$ is $$$i = 1$$$. Then $$$f(1, i) = f(1, 1) = \operatorname{MEX}([a_1]) = \operatorname{MEX}([1]) = 0$$$, and $$$f(i + 1, n) = f(2, 2) = \operatorname{MEX}([a_2]) = \operatorname{MEX}([0]) = 1$$$. Since $$$0 \neq 1$$$, the condition is satisfied.
In the second example, it can be shown that there is no way to reorder $$$a$$$ to satisfy the condition. As an example, consider the order $$$a = [3, 0, 0]$$$ and $$$i = 2$$$. We have $$$f(1, i) = f(1, 2) = \operatorname{MEX}([a_1, a_2]) = \operatorname{MEX}([3, 0]) = 1$$$, and $$$f(i + 1, n) = f(3, 3) = \operatorname{MEX}([a_3]) = \operatorname{MEX}([0]) = 1$$$, hence $$$f(1, i) = f(i + 1, n)$$$, so the choice of reordering is invalid.
In the third example, we can reorder $$$a$$$ into $$$[1, 6, 1, 0, 0, 5]$$$. When $$$i = 4$$$, $$$f(1, i) = f(1, 4) = \operatorname{MEX}([a_1, a_2, a_3, a_4]) = \operatorname{MEX}([1, 6, 1, 0]) = 2$$$, $$$f(i + 1, n) = f(5, 6) = \operatorname{MEX}([a_5, a_6]) = \operatorname{MEX}([0, 5]) = 1$$$, so the condition is satisfied for this $$$i$$$. It can be verified that the condition is also satisfied for all other $$$i$$$.
Alice and Bob play a game on a binary string $$$s$$$ of length $$$n$$$ (a string consisting only of characters $$$\mathtt{0}$$$ and $$$\mathtt{1}$$$). Alice moves first, and the players take alternate turns.
In one turn, a player chooses a sequence of indices $$$i_1, i_2, \ldots, i_m$$$ ($$$1 \le i_1 \lt i_2 \lt \ldots \lt i_m \le n$$$) such that the characters at these positions form a non-increasing sequence (that is, $$$s_{i_1} \ge s_{i_2} \ge \ldots \ge s_{i_m}$$$). The player then rearranges the characters at these positions to be sorted in non-decreasing order.
Formally, let the chosen characters consist of $$$z$$$ zeros and $$$o$$$ ones (where $$$z + o = m$$$). The move replaces the characters at positions $$$i_1, i_2, \ldots, i_m$$$ with a sequence of $$$z$$$ zeros followed by $$$o$$$ ones. A move is valid if and only if it strictly modifies the string $$$s$$$ (which implies $$$z \ge 1$$$ and $$$o \ge 1$$$).
The player who cannot make a valid move loses.
Assuming both players play optimally, determine the winner. If Alice wins, output a valid first move that is part of a winning strategy.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each test case consists of a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the string $$$s$$$.
The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of only characters $$$\mathtt{0}$$$ and $$$\mathtt{1}$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output one or three lines:
The sequence of indices you print must form a valid move according to the rules described in the statement. The indices must be printed in increasing order. If there are multiple winning moves, you may output any of them.
330002103101
BobAlice21 2Alice21 2
In the first example, there is no way to make a move after which $$$s$$$ will change, so Bob wins immediately.
In the third example, Alice can choose a subsequence $$$[1, 2]$$$ and sort it, after which $$$s$$$ will turn into $$$\mathtt{011}$$$. After that, Bob can't make a move, so Alice wins.
This is the easy version of the problem. The difference between the versions is that in this version, you only need to evaluate for whole string $$$s$$$, $$$s$$$ is regular, and the constraints on $$$n$$$ are higher.
We say that a bracket sequence $$$a$$$ is better than a bracket sequence $$$b$$$ if one of the following holds:
You are given a regular bracket sequence$$$^{\text{∗}}$$$ $$$s$$$ of even length $$$n$$$.
Among all non-empty subsequences $$$^{\text{†}}$$$ $$$t$$$ of $$$s$$$ that are regular bracket sequences, find the maximum possible length of $$$t$$$ such that $$$t$$$ is better than $$$s$$$. If no such $$$t$$$ exists, report it.
$$$^{\text{∗}}$$$A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting the characters $$$\texttt{1}$$$ and $$$\texttt{+}$$$ between the original characters of the sequence. For example:
$$$^{\text{†}}$$$A sequence $$$a$$$ is a subsequence of a sequence $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by the deletion of several (possibly, zero or all) element from arbitrary positions.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$n$$$ is even) — the length of the string $$$s$$$.
The second line of each test case contains a sequence $$$s$$$ of length $$$n$$$ consisting only of characters $$$\texttt{(}$$$ and $$$\texttt{)}$$$.
It is guaranteed that the given sequence $$$s$$$ is a regular bracket sequence.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print a single integer — the maximum possible length of a non-empty subsequence $$$t$$$ of $$$s$$$ that is a regular bracket sequence and is better than $$$s$$$. If no such $$$t$$$ exists, print $$$-1$$$.
32()8(()(()))6(())()
-16-1
In the first example, the only non-empty regular bracket subsequence of $$$s$$$ is $$$t = s = \texttt{()}$$$. Since $$$t$$$ is not better than $$$s$$$, we output $$$-1$$$.
In the second example, we can choose $$$t = \texttt{((()))}$$$. The first index where $$$t$$$ and $$$s$$$ differ is $$$i = 3$$$. Since $$$t_3 = \texttt{(}$$$ and $$$s_3 = \texttt{)}$$$, $$$t$$$ is better than $$$s$$$. We cannot choose a longer subsequence because the only longer regular bracket subsequence is $$$s$$$ itself, which is not better than $$$s$$$. Thus, we output $$$6$$$.
This is the hard version of the problem. The difference between the versions is that in this version, you need to find the sum of scores over all subsequences of $$$s$$$; $$$s$$$ is not necessarily a regular bracket sequence, and the constraints on $$$n$$$ are lower.
We say that a bracket sequence $$$a$$$ is better than a bracket sequence $$$b$$$ if one of the following holds:
For an arbitrary bracket sequence $$$t$$$, we define its score in the following way:
In other words, the score of $$$t$$$ is the length of the longest regular bracket subsequence of $$$t$$$ which is better than $$$t$$$. If $$$t$$$ is not a regular bracket sequence, or if no regular subsequence better than $$$t$$$ exists, the score is $$$0$$$.
You are given a bracket sequence $$$s$$$ of length $$$n$$$. Find the sum of the scores of all non-empty subsequences of $$$s$$$ modulo $$$998\,244\,353$$$.
$$$^{\text{∗}}$$$A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting the characters $$$\texttt{1}$$$ and $$$\texttt{+}$$$ between the original characters of the sequence. For example:
$$$^{\text{†}}$$$A sequence $$$a$$$ is a subsequence of a sequence $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by the deletion of several (possibly, zero or all) element from arbitrary positions.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 30$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$) — the length of the string $$$s$$$.
The second line of each test case contains a sequence $$$s$$$ of length $$$n$$$ consisting only of characters $$$\texttt{(}$$$ and $$$\texttt{)}$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
For each test case, print a single integer — the sum of the scores of all subsequences of $$$s$$$, modulo $$$998\,244\,353$$$.
51(6()()()6(())()8(())()()22()()())()()(()()()((()
04022563070
In the first example, the only non-empty subsequence is $$$g = \texttt{(}$$$. It is not a regular bracket sequence, so its score is $$$0$$$, and the total sum is also $$$0$$$.
In the second example, consider $$$g = s = \texttt{()()()}$$$. It is a regular bracket sequence. We can choose $$$r = \texttt{(())}$$$, which is a subsequence of $$$g$$$. The first index where $$$r$$$ and $$$g$$$ differ is $$$i = 2$$$. Since $$$r_2 = \texttt{(}$$$ and $$$g_2 = \texttt{)}$$$, $$$r$$$ is better than $$$g$$$. Hence, the score of $$$g$$$ is $$$|r| = 4$$$. All the other non-empty subsequences of $$$s$$$ have scores equal to $$$0$$$.
This is an interactive problem.
For a permutation$$$^{\text{∗}}$$$ $$$g = (g_1, g_2, \ldots, g_m)$$$, define its reverse as $$$\operatorname{rev}(g) = (g_m, g_{m-1}, \ldots, g_1)$$$.
There is a hidden permutation $$$p$$$ of size $$$n$$$. Your task is to find the lexicographically smallest$$$^{\text{†}}$$$ permutation $$$q$$$ of size $$$n$$$ such that $$$q \gt p$$$ and $$$\operatorname{rev}(q) \gt \operatorname{rev}(p)$$$.
However, you do not need to output $$$q$$$ itself. Instead, you must output a permutation $$$r$$$ of size $$$n$$$ such that $$$q_i = p_{r_i}$$$ for all $$$1 \le i \le n$$$. If no such $$$q$$$ exists, report that instead.
To find the answer, you can ask at most $$$3n$$$ queries of the form $$$\texttt{? } i \; j$$$ ($$$1 \le i, j \le n$$$). The interactor responds with $$$1$$$ if $$$p_i \lt p_j$$$ and $$$0$$$ otherwise. The interactor is not adaptive, which means that the permutation $$$p$$$ is fixed throughout the interaction.
$$$^{\text{∗}}$$$A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).
$$$^{\text{†}}$$$ An array $$$x$$$ is lexicographically smaller than array $$$y$$$ (so $$$x \lt y$$$) if and only if one of the following holds:
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^4$$$) — the size of the hidden permutation $$$p$$$.
The hidden permutation $$$p$$$ is fixed for the test case and does not change during the interaction. In other words, the interactor is not adaptive.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^4$$$.
For each test case, first read a single integer $$$n$$$.
You may ask up to $$$3n$$$ queries in each test case.
To ask a query, print a line in the format $$$\texttt{?} \; i \; j$$$ ($$$1 \le i, j \le n$$$).
As a response to the query, you will get:
If your program makes more than $$$3n$$$ queries for one test case, or makes an invalid query, then the response to the query will be "-1". After receiving such a response, your program should immediately terminate to receive the verdict Wrong Answer. Otherwise, it may receive any other verdict.
When you determine the answer for the current test case, print one of the following:
Note that this output does not count towards the query limit of $$$3n$$$.
After that, proceed to the next test case or terminate your program if there are no more test cases.
After printing each query do not forget to output the end of line and flush$$$^{\text{∗}}$$$ the output. Otherwise, you will get Idleness limit exceeded verdict.
If, at any interaction step, you read $$$-1$$$ instead of valid data, your solution must exit immediately. This means that your solution will receive Wrong answer because of an invalid query or any other mistake. Failing to exit can result in an arbitrary verdict because your solution will continue to read from a closed stream.
Note that the interactor is not adaptive, which means that the hidden permutation is unchanged throughout the interaction.
Hacks
To hack, use the following format.
The first line should contain a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases.
The first line of each test case should contain a positive integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^4$$$) — the size of $$$p$$$.
The second line of each test case should contain $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \le p_i \le n$$$).
You have to guarantee that given $$$p$$$ is a permutation and that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^4$$$.
$$$^{\text{∗}}$$$To flush, use:
3 5 1 4 1 0 1 0 9
? 2 1 ! 1 2 4 5 3 ? 2 3 ? 1 3 ? 1 4 ? 2 2 ! -1 ! 1 2 3 6 7 4 5 8 9
In the example, the interaction proceeds as follows.
| Solution | Jury | Explanation |
| 3 | There are $$$3$$$ test cases. | |
| 5 | In the first test case, the hidden permutation is $$$[5, 4, 2, 3, 1]$$$, with length $$$5$$$. The corresponding permutation $$$q = [5, 4, 3, 1, 2]$$$ | |
| ? 2 1 | 1 | The solution asks whether $$$p_2 \lt p_1$$$, and the jury responds with $$$1$$$, as $$$4 \lt 5$$$. |
| ! 1 2 4 5 3 | The solution has miraculously determined the answer and prints $$$r = [1, 2, 4, 5, 3]$$$, because, for instance, $$$q_3 = p_{r_3} = p_4 = 3$$$ and $$$q_2 = p_{r_2} = p_2 = 4$$$. | |
| 4 | In the second test case, the hidden permutation is $$$[3, 1, 2, 4]$$$, with length $$$4$$$. The permutation $$$q$$$ does not exist for that case. | |
| ? 2 3 | 1 | The solution asks whether $$$p_2 \lt p_3$$$, and the jury responds with $$$1$$$. |
| ? 1 3 | 0 | The solution asks whether $$$p_1 \lt p_3$$$, and the jury responds with $$$0$$$. |
| ? 1 4 | 1 | The solution asks whether $$$p_1 \lt p_4$$$, and the jury responds with $$$1$$$. |
| ? 2 2 | 0 | The solution asks whether $$$p_2 \lt p_2$$$, and the jury responds with $$$0$$$. |
| ! -1 | The solution now knows that $$$p = [3, 1, 2, 4]$$$ and determines that the corresponding $$$q$$$ does not exist, so it prints $$$-1$$$. | |
| 9 | In the third test case, $$$p = [8, 1, 9, 3, 5, 4, 2, 6, 7]$$$, with length $$$n = 9$$$. | |
| ! 1 2 3 6 7 4 5 8 9 | The solution decides to guess the answer and outputs $$$r = [1, 2, 3, 6, 7, 4, 5, 8, 9]$$$, which turns out to be correct. |
Note that the empty lines in the example input and output are for the sake of clarity and do not occur in the real interaction.
For a tree $$$T$$$ with $$$n \ge 2$$$ vertices, consider the standard process for generating its Prufer sequence. We repeatedly perform the following steps until only two vertices remain:
It is known that vertex $$$n$$$ is always one of the two remaining vertices. Let $$$v$$$ be the other remaining vertex. We define the Prufer vertex of $$$T$$$ as $$$P(T) = v$$$.
You are given a forest with $$$n$$$ vertices and $$$m$$$ edges. Let $$$k$$$ be the number of connected components in this forest, and let their sizes be $$$s_1, s_2, \ldots, s_k$$$. It is known that there are exactly $$$n^{k - 2} \prod\limits_{i=1}^k s_i$$$ ways to add edges to the forest so that it becomes a single tree.
For each $$$v$$$ ($$$1 \le v \lt n$$$), calculate how many of these ways result in a tree $$$T$$$ satisfying $$$P(T) = v$$$.
Since the answers can be large, print them modulo $$$998\,244\,353$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$0 \le m \le n - 1$$$) — the number of vertices and edges in the forest, respectively.
The next $$$m$$$ lines of each test case contain two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n, u \neq v$$$), describing an edge between vertices $$$u$$$ and $$$v$$$. It is guaranteed that these edges form a forest (that is, the graph is acyclic).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print $$$n - 1$$$ integers on a single line. The $$$i$$$-th integer should be the number of ways to add edges to the forest so that it becomes a tree $$$T$$$ satisfying $$$P(T) = i$$$, modulo $$$998\,244\,353$$$.
33 05 44 23 41 24 56 31 66 42 1
1 20 0 0 112 0 1 6 5
In the first example, there are no edges in the forest, and there are $$$3$$$ ways to complete it to a tree. One of them is to add edges $$$(1, 2)$$$ and $$$(1, 3)$$$. There are $$$2$$$ leaves: vertex $$$2$$$ and $$$3$$$. Since $$$2$$$ has a smaller label, it gets deleted from the tree, and only two vertices remain: $$$1$$$ and $$$3$$$. Therefore, the Prufer vertex of this tree is $$$1$$$.
In the third example, the forest is shown below:

Suppose we add edges $$$(3, 5)$$$ and $$$(3, 1)$$$ to complete it to a tree $$$T$$$. It is going to look like this:

It can be shown that $$$P(T) = 1$$$.
For a permutation$$$^{\text{∗}}$$$ $$$q$$$ of size $$$m \ge 3$$$, define $$$f(q)$$$ to be a sequence $$$b$$$ of size $$$m - 2$$$ such that $$$b_i = \operatorname{med}(q_i, q_{i + 1}, q_{i + 2})$$$ for all $$$1 \le i \le m - 2$$$. Here, $$$\operatorname{med}(x, y, z)$$$ denotes the second smallest element among $$$\{x, y, z\}$$$.
You are given an array $$$a$$$ of size $$$n$$$, where some elements may be $$$0$$$. It is guaranteed that $$$a$$$ contains the values $$$1$$$ and $$$n$$$ (that is, there exist indices $$$i, j$$$ such that $$$a_i = 1$$$ and $$$a_j = n$$$).
Find the number of permutations $$$p$$$ of size $$$n$$$ satisfying the following conditions:
Since the answer can be large, print it modulo $$$998\,244\,353$$$.
$$$^{\text{∗}}$$$A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 2 \cdot 10^5$$$) — the size of the array.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le n$$$).
It is guaranteed that all non-zero elements of $$$a$$$ are pairwise distinct. It is also guaranteed that $$$a$$$ contains the values $$$1$$$ and $$$n$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print a single integer — the number of permutations $$$p$$$ satisfying the conditions, modulo $$$998\,244\,353$$$.
531 3 250 5 4 1 070 0 1 0 0 7 0101 10 0 0 0 0 0 0 0 0150 0 10 0 0 15 0 0 6 7 0 1 0 0 3
101014
In the first example, the only permutation consistent with the input is $$$p = [1, 3, 2]$$$. We have $$$f(p) = [2]$$$, since $$$\operatorname{med}(1, 3, 2) = 2$$$. The elements of $$$f(p)$$$ are distinct, so this permutation is valid. The answer is $$$1$$$.
In the second example, there are two permutations consistent with the input: $$$p = [3, 5, 4, 1, 2]$$$ and $$$p = [2, 5, 4, 1, 3]$$$.