Circular

AtCoder
IDtenka1_2018_f
Time2000ms
Memory256MB
Difficulty
You are given a sequence of $N$ integers: $A_1,A_2,...,A_N$. Find the number of permutations $p_1,p_2,...,p_N$ of $1,2,...,N$ that can be changed to $A_1,A_2,...,A_N$ by performing the following operation some number of times (possibly zero), modulo $998244353$: * For each $1\leq i\leq N$, let $q_i=min(p_{i-1},p_{i})$, where $p_0=p_N$. Replace the sequence $p$ with the sequence $q$. ## Constraints * $1 \leq N \leq 3 × 10^5$ * $1 \leq A_i \leq N$ * All values in input are integers. ## Input Input is given from Standard Input in the following format: $N$ $A_1$ $:$ $A_N$ [samples]
Samples
Input #1
3
1
2
1
Output #1
2

$(2,3,1)$ and $(3,2,1)$ satisfy the condition.
Input #2
5
3
1
4
1
5
Output #2
0
Input #3
8
4
4
4
1
1
1
2
2
Output #3
24
Input #4
6
1
1
6
2
2
2
Output #4
0
API Response (JSON)
{
  "problem": {
    "name": "Circular",
    "description": {
      "content": "You are given a sequence of $N$ integers: $A_1,A_2,...,A_N$. Find the number of permutations $p_1,p_2,...,p_N$ of $1,2,...,N$ that can be changed to $A_1,A_2,...,A_N$ by performing the following opera",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "tenka1_2018_f"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a sequence of $N$ integers: $A_1,A_2,...,A_N$.\nFind the number of permutations $p_1,p_2,...,p_N$ of $1,2,...,N$ that can be changed to $A_1,A_2,...,A_N$ by performing the following opera...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments