AT_abc478_f [ABC478F] Min-First Search

Description

For a tree $ T $ with $ N $ vertices called vertex $ 1 $ , vertex $ 2 $ , $ \ldots $ , vertex $ N $ , let $ f(T) $ be the permutation $ P $ of $ (1,2,\ldots,N) $ obtained by the following procedure. - Initially, let the sequence $ P $ be the empty sequence $ () $ , and let the set $ S $ be $ \lbrace1\rbrace $ . - Repeat the following $ N $ times. - Let $ x $ be the minimum value of $ S $ . Remove $ x $ from $ S $ , and append $ x $ to the end of $ P $ . - Then, let $ v_1, v_2, \ldots, v_k $ be the vertices adjacent to vertex $ x $ in $ T $ . For $ i=1,2,\ldots,k $ , if $ v _ i $ is not contained in $ P $ , add $ v _ i $ to $ S $ . You are given a permutation $ Q=(Q _ 1,Q _ 2,\ldots,Q _ N) $ of $ (1,2,\ldots,N) $ . Find the number, modulo $ 998244353 $ , of trees $ T $ such that $ f(T)=Q $ . Here, two trees are distinguished when there exists a pair of vertices $ (u,v) $ such that there is an edge between vertex $ u $ and vertex $ v $ in one tree and there is no edge in the other tree.

Input Format

The input is given from Standard Input in the following format: > $ N $ $ Q _ 1 $ $ Q _ 2 $ $ \ldots $ $ Q _ N $

Output Format

Output the number, modulo $ 998244353 $ , of trees $ T $ such that $ f(T)=Q $ .

Explanation/Hint

### Sample Explanation 1 For example, if $ T $ is the following tree, then $ f(T)=Q $ . ![](https://cdn.luogu.com.cn/upload/vjudge_pic/AT_abc478_f/3328e6721bb00235cb7a3be72d6416afd4420ea43e8f2bae852d71bc3e01329a.png) The procedure to find $ f(T) $ proceeds as shown in the following figure. ![](https://cdn.luogu.com.cn/upload/vjudge_pic/AT_abc478_f/45e57a8d719ac5ba1e00de6f030a56a64d0ba55b18ee266e2d3f2bdce6741842.png) Including this tree, the following eight trees satisfy the condition. ![](https://cdn.luogu.com.cn/upload/vjudge_pic/AT_abc478_f/8c1849f68b4f6b4c1f10da155f3966987a71f61b9545216fba67c64e5a153efb.png) Thus, output `8`. ### Sample Explanation 2 Output modulo $ 998244353 $ the number of trees satisfying the condition. ### Constraints - $ 1\le N\le2\times10 ^ 5 $ - $ 1\le Q _ i\le N\ (1\le i\le N) $ - $ Q _ i\ne Q _ j\ (1\le i\lt j\le N) $ - $ Q _ 1=1 $ - All input values are integers.