#P9307. Permutation Pair Reordering

    ID: 22462 Type: Default 1000ms 256MiB

Permutation Pair Reordering

Permutation Pair Reordering

We are given a sequence \(a\) of length \(n\), where each element \(a_i\) is a pair \((p_i, q_i)\). It is known that \(p=[p_1, p_2,\dots,p_n]\) and \(q=[q_1,q_2,\dots,q_n]\) are both permutations of \(\{1,2,\dots,n\}\).

Define the weight function of a sequence \(a\) as \[ f(a)=\sum_{i=1}^n \Bigl( [p_i>\max_{1\le j\max_{1\le j<i}q_j] \Bigr), \] where for \(i=1\) we set \(\max_{1\le j<1}p_j=\max_{1\le j<1}q_j=-\infty\) so that both brackets count as 1. (Here \([statement]\) equals 1 if the statement is true, and 0 otherwise.)

Now, we are allowed to arbitrarily reorder the sequence \(a\) (that is, we choose a permutation \(\sigma\) so that the new sequence is \(a'_i=a_{\sigma(i)}\), the pair is taken as an integral whole). Let \(f(a')\) be computed from the new ordering in the same way. We wish to choose an ordering that maximizes \(f(a')\).

One may show that if the maximum number of indices which are records in both coordinates (i.e. those indices \(i\) for which simultaneously \(p'_i>\max_{j\max_{j<i}q'_j\)) is \(L\) then the maximum possible value is \[ f(a')_{\max}=n+L. \] In other words, an optimal reordering is exactly one in which every element is a record in at least one coordinate, and exactly \(L\) elements are records in both coordinates.

Your task is to compute \(f(a')_{\max}\) and the number of reorderings \(a'\) that achieve this optimum, modulo \(998244353\).

inputFormat

The input consists of three lines:

  • The first line contains a single integer (n) (the number of pairs).
  • The second line contains (n) space‐separated integers (p_1, p_2, \dots, p_n) forming a permutation of ({1,2,\dots,n}).
  • The third line contains (n) space‐separated integers (q_1, q_2, \dots, q_n) forming a permutation of ({1,2,\dots,n}).

outputFormat

Output two space‐separated integers: the maximum value of (f(a')) and the number of reorderings that achieve this value, taken modulo (998244353).

sample

1
1
1
2 1