ZSUM - Just Add It

For two given integers n and k find (Zn + Zn-1 - 2Zn-2) mod 10000007, where Zn = Sn + Pn and Sn = 1k + 2k + 3k + … + nk and Pn = 11 + 22 + 33 + … + nn.

Input

There are several test cases (≤ 10000). In each case two space separated positive integers n and k are given.
For last test case n and k are given as 0 0, which is not to be processed.

Constraints

1 < n < 200000000
0 < k < 1000000

Output

For each case print the asked value in separate line.

Example

Input:
10 3
9 31
83 17
5 2
0 0

Output:
4835897
2118762
2285275
3694

Added by:Manohar Singh
Date:2011-09-04
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Manohar Singh

hide comments
2017-01-23 15:00:18
TLE in .py AC in C
2017-01-14 10:34:57
No way to get past TLE unless you use modular exponentiation
2016-12-24 13:18:32
modular exponentiation
AC in on go.............my 50th :-)
2016-12-15 12:15:28
learned a new concept !!
2016-12-10 10:28:42
TLE in JAVA :/
2016-11-05 19:34:35
TLE with java with correct logic
2016-10-04 16:17:48
its some what tricky but but biginners must try it
2016-09-24 19:04:10 Abhishek
please increase time limit for python, or remove it from the available languages
2016-08-31 14:40:54
dont get nervous by looking at the description of question... Its very easy when you simplify the equation.
GO FOR IT. YOU WILL LEARN SOMETHING NEW.
2016-08-16 14:47:58
Easy one if u do a little bit of pen paper work !!! mod=10^7+7..........
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.