---
{"category_name":"easy","problem_code":"OPTSORT","problem_name":"Optimal Sorting","problemComponents":{"constraints":"- $1 \\leq T \\leq 10^5$\n- $1 \\leq N \\leq 10^5$\n- $1 \\leq A_i \\leq 10^9$\n- Sum of $N$ over all test cases does not exceed $2 \\cdot 10^5$\n","constraintsState":true,"subtasks":"- **Subtask $1$ (20 points):** $N \\leq 1000;$ $\\Sigma{N} \\leq 2000$\n- **Subtask $2$ (40 points):** $A$ is a permutation from $1$ to $N$\n- **Subtask $3$ (40 points):** Original constraints","subtasksState":true,"inputFormat":"- The first line of the input contains a single integer $T$ - the number of test cases. The test cases then follow.\n- The first line of each test case contains a single integer $N$.\n- The second line of each test case contains $N$ space-separated integers $A_1, A_2, \\ldots, A_N$.","inputFormatState":true,"outputFormat":"- For each test case, print the minimum total cost of operations so that the sequence $A$ becomes sorted in **non-descending** order.","outputFormatState":true,"sampleTestCases":{"0":{"id":1,"input":"3\n4\n1 3 6 7\n3\n10 1 4\n6\n3 1 3 3 4 3","output":"0\n9\n3","explanation":"- **Test case $1$:** The sequence is already sorted in non-descending order. So, we require $0$ operations and thus $0$ total cost.\n\n- **Test case $2$:** We can apply the operation on $A_{1 \\ldots 3}$ which converts $A$ into $[\\textcolor{blue}{1,4,10}]$ which is sorted in non-descending order.\nSo, the total cost is $10-1=9$. It can be shown that we can\u0027t sort $A$ with less than $9$ total cost.\n\n- **Test Case $3$:** First, we can apply the operation on $A_{1 \\ldots 4}$ which converts $A$ into $[\\textcolor{blue}{1,3,3,3},4,3]$.\nThen, apply the operation on $A_{3 \\ldots 6}$ which converts $A$ into $[1,3,\\textcolor{blue}{3,3,3,4}]$ which is sorted in non-descending order.\nSo, the total cost is $3-1 + 4-3 = 3$. It can be shown that we can\u0027t sort $A$ with less than $3$ total cost.","isDeleted":false}}},"video_editorial_url":"","languages_supported":{"0":"CPP14","1":"C","2":"JAVA","3":"PYTH 3.6","4":"CPP17","5":"PYTH","6":"PYP3","7":"CS2","8":"ADA","9":"PYPY","10":"TEXT","11":"PAS fpc","12":"NODEJS","13":"RUBY","14":"PHP","15":"GO","16":"HASK","17":"TCL","18":"PERL","19":"SCALA","20":"LUA","21":"kotlin","22":"BASH","23":"JS","24":"LISP sbcl","25":"rust","26":"PAS gpc","27":"BF","28":"CLOJ","29":"R","30":"D","31":"CAML","32":"FORT","33":"ASM","34":"swift","35":"FS","36":"WSPC","37":"LISP clisp","38":"SQL","39":"SCM guile","40":"PERL6","41":"ERL","42":"CLPS","43":"ICK","44":"NICE","45":"PRLG","46":"ICON","47":"COB","48":"SCM chicken","49":"PIKE","50":"SCM qobi","51":"ST","52":"SQLQ","53":"NEM"},"max_timelimit":1,"source_sizelimit":50000,"problem_author":"akshitm16","problem_tester":"IceKnight1093","date_added":"21-12-2021","tags":{"0":"akshitm16","1":"easy","2":"ltime103"},"problem_difficulty_level":"Easy","best_tag":"","editorial_url":"https://discuss.codechef.com/problems/OPTSORT","time":{"view_start_date":1640453400,"submit_start_date":1640453400,"visible_start_date":1640453400,"end_date":1735669800},"is_direct_submittable":false,"problemDiscussURL":"https://discuss.codechef.com/search?q=OPTSORT","is_proctored":false,"visitedContests":{},"layout":"problem"}
---
This is an example statement in markdown. This is the statement of the problem [TANDC](https://codechef.com/problems/TANDC) on CodeChef. The main statement starts with the next paragraph. Please make sure to remove this paragraph from your statement before publishing your problem. If your problem doesn't contain Subtasks, you should remove the section subtask too. We are using markdown the syntax of which can be seen [at](https://github.com/showdownjs/showdown/wiki/Showdown's-Markdown-syntax). We request you to not use any HTML tags in the statement, e.g. no p, ul, li, pre, br or b tags. If you face any issue, please contact admins or email us at help@codechef.com.
Tracy is teaching Charlie maths via a game called $N$-Cube, which involves three sections involving $N$.
Tracy gives Charlie a number $N$, and Charlie makes a list of $N$-th powers of integers in increasing order $1^N, 2^N, 3^N, \dot, \text{so on}$. This teaches him exponentiation.
Then Charlie performs the following subtraction game $N$ times: Take all pairs of consecutive numbers in the list and take their difference. These differences then form the new list for the next iteration of the game. Eg, if $N$ was 6, the list proceeds as $[1, 64, 729, 4096 ... ]$ to $[63, 685, 3367 ...]$, and so on $5$ more times.
After the subtraction game, Charlie has to correctly tell Tracy the $N$-th element of the list. This number is the *value of the game*.
After practice Charlie became an expert in the game. To challenge him more, Tracy will give two numbers $M$ (where $M$ is a prime) and $R$ instead of just a single number $N$, and the game must start from $M_R - 1$ instead of $N$. Since the *value of the game* can now become large, Charlie just have to tell the largest integer $K$ such that $M_K$ divides this number. Since even $K$ can be large, output $K$ modulo 1000000007 ($10^9 + 7$).
<aside style='background: #f8f8f8;padding: 10px 15px;'><div>All submissions for this problem are available.</div></aside>