Unique Matching

AtCoder
IDagc067_d
Time2000ms
Memory256MB
Difficulty
You are given an integer $N$ and prime $P$. We call a sequence of $N$ intervals $([L_1,R_1] ,[L_2,R_2], \cdots, [L_N,R_N])$ **good** if and only if both of the followings are satisfied: * $1\le L_i\le R_i\le N$ holds for all $1\le i\le N$. * There exists a **unique** permutation $x=(x_1,x_2,\cdots,x_N)$ of $(1,2,\cdots,N)$ such that $L_i\le x_i\le R_i$ holds for all $1\le i\le N$. Count the number, modulo $P$, of **good** sequences of intervals. ## Constraints * $2 \leq N \leq 5000$ * $10^9<P<1.01\times 10^9$ * $P$ is prime. * All input values are integers. ## Input Input is given from Standard Input in the following format: $N$ $P$ [samples]
Samples
Input #1
2 1005488041
Output #1
6

The following $6$ sequences are good:

*   $([1,1],[2,2])$
*   $([1,2],[2,2])$
*   $([1,1],[1,2])$
*   $([2,2],[1,1])$
*   $([2,2],[1,2])$
*   $([1,2],[1,1])$
Input #2
5 1005488041
Output #2
102960
Input #3
100 1005488041
Output #3
47599495
Input #4
1000 1005488041
Output #4
632708165
API Response (JSON)
{
  "problem": {
    "name": "Unique Matching",
    "description": {
      "content": "You are given an integer $N$ and prime $P$. We call a sequence of $N$ intervals $([L_1,R_1] ,[L_2,R_2], \\cdots, [L_N,R_N])$ **good** if and only if both of the followings are satisfied: *   $1\\le L_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": "agc067_d"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given an integer $N$ and prime $P$.\nWe call a sequence of $N$ intervals $([L_1,R_1] ,[L_2,R_2], \\cdots, [L_N,R_N])$ **good** if and only if both of the followings are satisfied:\n\n*   $1\\le L_i...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments