Coloring Book
Time limit1sMemory limit128 MB
Count the assignments of K colors to N pictures so each picture i differs from picture f_i unless f_i equals i, modulo 1,000,000,007.
- Level
Medium6 of 10
- Topics
- Graph, Combinatorics, Math
- Solved
- No attempts yet
Problem
Sanggeun colors pictures whenever he has spare time. He has a palette holding colors and one brush. His friend Seonyeong gave him a coloring book for his birthday. The book has pictures, numbered 1 through .
Sanggeun wants to paint every picture with one of the colors. Seonyeong likes flashy results, so she fixed numbers . Picture must be painted a different color from picture . When and are the same, picture can be painted with no restriction.
Given , , and every , write a program that counts the ways Sanggeun can color the book.
Input
The first line contains and . ()
The second line contains the numbers . ()
Output
Print the number of ways to color the coloring book on the first line. The count gets very large, so print it modulo .
Hint
When , , and , pictures 1 and 2 cannot take the same color. Writing the two colors as an ordered pair, the six possibilities are (1,2), (1,3), (2,1), (2,3), (3,1), (3,2).