Fox's Dream
Time limit2sMemory limit1024 MB
Given N disks that all start on peg K, find the minimum number of moves to reach the target peg for each disk, modulo 1000000007.
Problem
The legend of Fox Village says that a wish comes true when all disks of a Tower of Hanoi with 3 pegs and disks are gathered on one peg.
Disks move under these rules:
- The radius of the -th disk is . ()
- At all times, the disks on one peg must be ordered so that smaller radii are on top.
- Only the top disk on each peg can be moved.
- A disk can only be placed on top of a peg.
- Only one disk can be moved at a time.
A baby fox in the village heard this legend and decided to try the Tower of Hanoi. The baby fox gathered all the disks on the -th peg and made a wish.
"Let me become the cutest fox in the world!"
Nothing changed for the baby fox.
Disappointed, the baby fox decided to arrange the disks into a shape it finds beautiful. Now a Tower of Hanoi master, the baby fox can move one disk per second. Find the minimum time, in seconds, that the baby fox needs to arrange the disks into the beautiful shape.
Input
The first line contains and , separated by a space. (; )
The second line contains integers separated by spaces. The -th number is the peg on which the -th disk sits in the beautiful shape. ()
The beautiful shape the baby fox imagined satisfies the rules of the Tower of Hanoi.
Output
Print, on the first line, the minimum time in seconds for the baby fox to build the beautiful shape, modulo . is a prime.
If the beautiful shape cannot be built, print -1.
Hint
Is the legend of Fox Village true?