问题3214--Good Serial Inc.

3214: Good Serial Inc.

[命题人 : ]
时间限制 : 1.000 sec  内存限制 : 64 MB

题目描述

Good Serial Inc. (GSI) produces integer sequences with length N, in which every element is an integer in range [1, M]. They call a sequence S is good if the sequence has the following property: for every substring of S with length M, i.e., S[ii+m-1], all the elements in the substring are the same or all the elements are distinct(different from each other). The company GIS is designed to produce good sequences. But how many different good sequences are there? Since the answer will be very large, just output the result after module 987654321.

输入

There are several cases. For each case, there is a line with two integers N, and M ( 1 ≤ N ≤ 1000000, 1 ≤ M ≤ 1000000 ). The input ends up with two negative numbers, which should not be processed as a case.

输出

Print the number of different good sequences module 987654321 in a line for each case.

样例输入 Copy

4 4
3 5
-1 -1

样例输出 Copy

28
125

来源/分类

multi