{"trustable":true,"prependHtml":"\u003cstyle type\u003d\u0027text/css\u0027\u003e\n .input, .output {\n border: 1px solid #888888;\n }\n .output {\n margin-bottom: 1em;\n position: relative;\n top: -1px;\n }\n .output pre, .input pre {\n background-color: #EFEFEF;\n line-height: 1.25em;\n margin: 0;\n padding: 0.25em;\n }\n \u003c/style\u003e\n \u003clink rel\u003d\"stylesheet\" href\u003d\"//codeforces.org/s/96598/css/problem-statement.css\" type\u003d\"text/css\" /\u003e\u003cscript\u003e window.katexOptions \u003d { disable: true }; \u003c/script\u003e\n\u003cscript type\u003d\"text/x-mathjax-config\"\u003e\n MathJax.Hub.Config({\n tex2jax: {\n inlineMath: [[\u0027$$$\u0027,\u0027$$$\u0027], [\u0027$\u0027,\u0027$\u0027]],\n displayMath: [[\u0027$$$$$$\u0027,\u0027$$$$$$\u0027], [\u0027$$\u0027,\u0027$$\u0027]]\n }\n });\n\u003c/script\u003e\n\u003cscript type\u003d\"text/javascript\" async src\u003d\"https://mathjax.codeforces.org/MathJax.js?config\u003dTeX-AMS_HTML-full\"\u003e\u003c/script\u003e","sections":[{"title":"","value":{"format":"HTML","content":"\u003cp\u003eInsertion sort is a simple sorting algorithm that builds the final sorted array one item at an iteration.\u003c/p\u003e\u003cp\u003eMore precisely, insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.\u003c/p\u003e\u003cp\u003eThis type of sorting is typically done in-place, by iterating up the array, growing the sorted array behind it. At each array-position, it checks the value there against the largest value in the sorted array (which happens to be next to it, in the previous array-position checked). If larger, it leaves the element in place and moves to the next. If smaller, it finds the correct position within the sorted array, shifts all the larger values up to make a space, and inserts into that correct position.\u003c/p\u003e\u003cp\u003eThe resulting array after $$$k$$$ iterations has the property where the first $$$k$$$ entries are sorted. In each iteration the first remaining entry of the input is removed, and inserted into the result at the correct position, thus extending the result.\u003c/p\u003e\u003cp\u003eKnuth is an ACM-ICPC master and provides a modified pseudocode implementation about the insertion sort for you. His modified algorithm for an array of sortable items $$$A$$$ ($$$1$$$-based array) can be expressed as:\u003c/p\u003e\u003ccenter\u003e \u003cimg class\u003d\"tex-graphics\" src\u003d\"CDN_BASE_URL/a1acdb3697f860e9eeeb2468d885bf63?v\u003d1715669765\" style\u003d\"max-width: 100.0%;max-height: 100.0%;\"\u003e \u003c/center\u003e\u003cp\u003eHe notes that a permutation of $$$1$$$ to $$$n$$$ is almost sorted if the length of its longest increasing subsequence is at least $$$(n - 1)$$$.\u003c/p\u003e\u003cp\u003eGiven the parameter $$$k$$$, you are asked to count the number of distinct permutations of $$$1$$$ to $$$n$$$ meeting the condition that, after his modified insertion sort, each permutation would become an almost sorted permutation.\u003c/p\u003e"}},{"title":"Input","value":{"format":"HTML","content":"\u003cp\u003eThe input contains several test cases, and the first line contains a positive integer $$$T$$$ indicating the number of test cases which is up to $$$5000$$$.\u003c/p\u003e\u003cp\u003eFor each test case, the only line contains three integers $$$n, k$$$ and $$$q$$$ indicating the length of the permutations, the parameter in his implementation and a prime number required for the output respectively, where $$$1 \\leq n, k \\leq 50$$$ and $$$10^8 \\leq q \\leq 10^9$$$.\u003c/p\u003e"}},{"title":"Output","value":{"format":"HTML","content":"\u003cp\u003eFor each test case, output a line containing \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eCase #x: y\u003c/span\u003e\" (without quotes), where \u003cspan class\u003d\"tex-font-style-tt\"\u003ex\u003c/span\u003e is the test case number starting from $$$1$$$, and \u003cspan class\u003d\"tex-font-style-tt\"\u003ey\u003c/span\u003e is the remainder of the number of permutations which meet the requirement divided by $$$q$$$.\u003c/p\u003e"}},{"title":"Examples","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e4\n4 1 998244353\n4 2 998244353\n4 3 998244353\n4 4 998244353\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003eCase #1: 10\nCase #2: 14\nCase #3: 24\nCase #4: 24\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"Note","value":{"format":"HTML","content":"\u003cp\u003eIn the first sample case, we can discover $$$10$$$ permutations which meet the condition, and they are listed as follows:\u003c/p\u003e\u003cul\u003e \u003cli\u003e $$$[1, 2, 3, 4]$$$; \u003c/li\u003e\u003cli\u003e $$$[1, 2, 4, 3]$$$; \u003c/li\u003e\u003cli\u003e $$$[1, 3, 2, 4]$$$; \u003c/li\u003e\u003cli\u003e $$$[1, 3, 4, 2]$$$; \u003c/li\u003e\u003cli\u003e $$$[1, 4, 2, 3]$$$; \u003c/li\u003e\u003cli\u003e $$$[2, 1, 3, 4]$$$; \u003c/li\u003e\u003cli\u003e $$$[2, 3, 1, 4]$$$; \u003c/li\u003e\u003cli\u003e $$$[2, 3, 4, 1]$$$; \u003c/li\u003e\u003cli\u003e $$$[3, 1, 2, 4]$$$; \u003c/li\u003e\u003cli\u003e $$$[4, 1, 2, 3]$$$. \u003c/li\u003e\u003c/ul\u003e"}}]}