bool is_sorted(int a[], int n) { for (int i = 1; i < n; i++) { if (a[i] < a[i - 1]) { return false; } } return true; } void bogo_sort(int a[], int n) { while (!is_sorted(a, n)) { shuffle(a, a + n); } }
int p[N] = {....}; // Tonnnny's favorite permutation of n void shuffle(int a[], int n) { int b[n]; for (int i = 0; i < n; i++) { b[i] = a[i] } for (int i = 0; i < n; i++) { a[i] = b[p[i]]; } } void tonnnny_sort(int a[], int n) { assert (n == N); // Tonnnny appointed! while (!is_sorted(a, n)) { shuffle(a, a + n); } }
Tonnnny was satsified with the new algorithm, and decided to let you give him a different array of length every day to sort it with Tonnnny Sort.
You are the best friend of Tonnnny. Even though you had found the algorithm is somehow wrong, you want to make Tonnnny happy as long as possible. You're given , and you need to calculate the maximum number of days that Tonnnny will be happy, since after that you can't give Tonnnny an array that can be sorted with Tonnnny Sort and didn't appeared before.
The answer may be very large. Tonnnny only like numbers with at most digits, so please output answer mod
instead.
The first line contains one integer.
The second line containsinteger indicating
, which forms a permutation of
.
The maximum number of days that Tonnnny will be happy, module.