Jiraiya and Rasengan Attacks
Practice
4.3 (17 votes)
Approved
Bit manipulation
Exception handling
Medium
Ready
Problem
84% Success 924 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Naruto has started training from Jiraiya to develop the most powerful Rasengan attack. Jiraiya as always uses weird training exercises.

enter image description here

Jiraiya has brought a permutation P of \(1,2..\space N\) integers. He tells Naruto to sort the permutation using minimum number of Rasengan attacks. In one Rasengan attack Naruto can choose any pair of consecutive elements of the permutation and swap it.

To make the task a little difficult, he chooses a subsegment of the permutation uniformly at random \(P_L, P_{L+1},.., P_R\) (L ≤ R) and reverses it to make \(P_1, P_2,..,P_{L-1}, P_R,.., P_{L+1}, P_L, P_{R+1},..,P_N\space \). Now he asks Naruto to tell the expected number of Rasengan attacks he need to sort the permutation.

INPUT
The first line contains the integer N.
The next line contains N integers denoting the permutation P.

OUTPUT
Print \((A * B^{-1}) \space mod\space 10^9+7\) where \(\frac{A}{B}\) is the Expected value of number of Rasengan attacks he need to sort P (expressed as an irreducible fraction)

CONSTRAINTS
\(2 ≤ N ≤ 10^5\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
8 votes
Tags:
Advanced Data StructuresAlgorithmsData StructuresFenwick TreeMediumOpen
Points:30
9 votes
Tags:
Advanced Data StructuresData StructuresFenwick TreeSegment Trees
Points:30
7 votes
Tags:
Medium