Swap

아직 제출이 없습니다시간 제한2초메모리 제한256 MB

문제

In this problem, we are interested in undirected graphs with nn vertices labeled by integers from 11 to nn, without loops and parallel edges.

For a given graph GG, the following procedure can be executed.

function dfs(u) {
  set u to active
  for v in 1, 2, ..., n
    if (u and v are adjacent in G) and (v is inactive) {
      swap P[u] and P[v]
      dfs(v)
    }
  set u to done
}

for u in 1, 2, ..., n {
  set u to inactive
  set P[u] to u
}
for u in 1, 2, ..., n
  if u is inactive:
    dfs(u)

Given a_1,a_2,,a_na\_1, a\_2, \ldots, a\_n, find the number of different graphs GG such that after the procedure, P\[1]=a_1P\[1] = a\_1, P\[2]=a_2P\[2] = a\_2, \ldots, and P\[n]=a_nP\[n] = a\_n. Two graphs are different if there is a pair of vertices (u,v)(u, v) such that one of them contains such edge and the other one does not. As the number of such graphs may be very large, find it modulo 998,244,353998\\,244\\,353.

입력

The first line contains an integer nn (1n5001 \leq n \leq 500).

The second line contains nn integers a_1,a_2,,a_na\_1, a\_2, \ldots, a\_n (1a_in1 \leq a\_i \leq n). It is guaranteed that each number from 11 to nn occurs in the sequence exactly once.

출력

Print one integer: the number of different graphs GG satisfying the conditions, modulo 998,244,353998\\,244\\,353.