Beautiful Kadomatsu

AtCoder
IDabc439_f
Time2000ms
Memory256MB
Difficulty
A sequence $a=(a_1,a_2,\dots,a_k)$ of length $k$ is defined to be **kadomatsu-like** as follows: * Let $x$ be the number of integers $i$ that satisfy $2 \le i \le k-1$, $a_{i-1} < a_i$, and $a_i > a_{i+1}$. * Let $y$ be the number of integers $i$ that satisfy $2 \le i \le k-1$, $a_{i-1} > a_i$, and $a_i < a_{i+1}$. * The sequence $a$ is called **kadomatsu-like** if and only if $x > y$. You are given a permutation $P$ of $(1,2,\dots,N)$. Find the number, modulo $998244353$, of (not necessarily contiguous) subsequences of $P$ that are kadomatsu-like. ## Constraints * All input values are integers. * $1 \le N \le 3 \times 10^5$ * $P$ is a permutation of $(1,2,\dots,N)$. ## Input The input is given from Standard Input in the following format: $N$ $P_1$ $P_2$ $\dots$ $P_N$ [samples]
Samples
Input #1
4
1 3 4 2
Output #1
4

Among the subsequences of $P$, the following four are kadomatsu-like:

*   $(1,3,4,2)$
*   $(1,3,2)$
*   $(1,4,2)$
*   $(3,4,2)$
Input #2
1
1
Output #2
0
Input #3
20
11 10 18 13 12 16 5 19 7 6 17 4 9 1 14 2 20 15 8 3
Output #3
431610

For example, the subsequence $a=(10,13,12,5,7,9,20,3)$ is kadomatsu-like.

*   We have $a_{i-1} < a_i$ and $a_i > a_{i+1}$ for $i=2,7$, so $x=2$.
*   We have $a_{i-1} > a_i$ and $a_i < a_{i+1}$ for $i=4$, so $y=1$.
*   Since $x > y$, the subsequence $a$ is kadomatsu-like.
API Response (JSON)
{
  "problem": {
    "name": "Beautiful Kadomatsu",
    "description": {
      "content": "A sequence $a=(a_1,a_2,\\dots,a_k)$ of length $k$ is defined to be **kadomatsu-like** as follows: *   Let $x$ be the number of integers $i$ that satisfy $2 \\le i \\le k-1$, $a_{i-1} < a_i$, and $a_i > ",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc439_f"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "A sequence $a=(a_1,a_2,\\dots,a_k)$ of length $k$ is defined to be **kadomatsu-like** as follows:\n\n*   Let $x$ be the number of integers $i$ that satisfy $2 \\le i \\le k-1$, $a_{i-1} < a_i$, and $a_i > ...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments