n, k = map(int, input().split()) mo = 10 ** 9 + 7 a, b = 1, 1 for _ in range(n): a = (a * k + a * (1 + k) * k // 2) % mo b = (b * (1 + k) * k // 2) % mo print((a - b) % mo)