시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 128 MB135725716319.854%

문제

상근이는 시간이 날 때마다 색칠 공부를 하고 있다. 상근이는 총 K개의 색이 담겨있는 팔레트를 가지고 있고, 붓을 이용해 색칠을 한다. 상근이의 친구 선영이는 상근이의 생일 선물로 색칠 공부 책을 선물했다. 책에는 그림이 총 N개 있으며, 1번부터 N번까지 번호가 붙여져 있다.

상근이는 각각의 그림을 K가지 색 중 하나를 골라 색칠하려고 한다. 선영이는 화려한 것을 좋아하기 때문에 상근이에게 숫자 N개 fi를 정해주었다. 상근이는 i번 그림을 fi번 그림과 다른 색으로 색칠해야 한다. i가 fi와 같은 경우에 아무런 제한없이 i번 그림을 색칠할 수 있다.

N과 K, 그리고 fi가 모두 주어졌을 때, 상근이가 색칠 공부 책을 색칠하는 방법의 수를 구하는 프로그램을 작성하시오.

입력

첫째 줄에 N과 K가 주어진다. (1 ≤ N, K ≤ 1,000,000) 둘째 줄에는 숫자 N개 fi가 주어진다. (1 ≤ fi ≤ N)

출력

첫째 줄에 색칠 공부 책을 색칠하는 방법의 수를 출력한다. 방법의 수가 매우 많기 때문에 1,000,000,007로 나눈 나머지를 출력한다. 

예제 입력 1

2 3
2 1

예제 출력 1

6

예제 입력 2

3 4
2 3 1

예제 출력 2

24

예제 입력 3

3 4
2 1 1

예제 출력 3

36

예제 입력 4

3 4
1 1 2

예제 출력 4

36

힌트

첫 번째 예제의 경우 1번 그림과 2번 그림은 서로 같은 색으로 색칠하면 안 된다. 따라서 가능한 방법은 (1,2), (1,3), (2,1), (2,3), (3,1), (3,2)가 있다.

W3sicHJvYmxlbV9pZCI6Ijk1MjEiLCJwcm9ibGVtX2xhbmciOiIwIiwidGl0bGUiOiJcdWMwYzlcdWNlNjAgXHVhY2Y1XHViZDgwIiwiZGVzY3JpcHRpb24iOiI8cD5cdWMwYzFcdWFkZmNcdWM3NzRcdWIyOTQgXHVjMmRjXHVhYzA0XHVjNzc0IFx1YjBhMCBcdWI1NGNcdW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BmaXJzdCBpbWFnZSwgaXQgY2FuIGJlIHBhaW50ZWQgaW4gd2hpY2hldmVyIGNvbG9yLiBUaGUgc2Vjb25kIG11c3QgYmUgZGlmZmVyZW50IHRoYW4gdGhlIGZpcnN0LCBhbmQgdGhlIHRoaXJkIGRpZmZlcmVudCB0aGFuIHRoZSBzZWNvbmQuIFRoYXQgbWVhbnMgdGhhdCB0aG9zZSB0d28gaW1hZ2VzIGNhbiBiZSBjb2xvcmVkIGluIHRoZSByZW1haW5pbmcgMyBjb2xvcnMuIFRoaXMgZ2l2ZXMgdXMgYSB0b3RhbCBvZiAzNiBjb21iaW5hdGlvbnMuPFwvcD5cclxuIiwib3JpZ2luYWwiOiIxIiwiaHRtbF90aXRsZSI6IjAiLCJwcm9ibGVtX2xhbmdfdGNvZGUiOiJFbmdsaXNoIn1d

출처

Contest > Croatian Open Competition in Informatics > COCI 2013/2014 > Contest #2 5번

  • 문제를 번역한 사람: baekjoon
  • 문제의 오타를 찾은 사람: uk7880