atcoder#ARC064D. [ARC064F] Rotated Palindromes

[ARC064F] Rotated Palindromes

Score : 10001000 points

Problem Statement

Takahashi and Aoki are going to together construct a sequence of integers.

First, Takahashi will provide a sequence of integers aa, satisfying all of the following conditions:

  • The length of aa is NN.
  • Each element in aa is an integer between 11 and KK, inclusive.
  • aa is a palindrome, that is, reversing the order of elements in aa will result in the same sequence as the original.

Then, Aoki will perform the following operation an arbitrary number of times:

  • Move the first element in aa to the end of aa.

How many sequences aa can be obtained after this procedure, modulo 109+710^9+7?

Constraints

  • 1N1091 \leq N \leq 10^9
  • 1K1091 \leq K \leq 10^9

Input

The input is given from Standard Input in the following format:

NN KK

Output

Print the number of the sequences aa that can be obtained after the procedure, modulo 109+710^9+7.

4 2
6

The following six sequences can be obtained:

  • (1,1,1,1)(1, 1, 1, 1)
  • (1,1,2,2)(1, 1, 2, 2)
  • (1,2,2,1)(1, 2, 2, 1)
  • (2,2,1,1)(2, 2, 1, 1)
  • (2,1,1,2)(2, 1, 1, 2)
  • (2,2,2,2)(2, 2, 2, 2)
1 10
10
6 3
75
1000000000 1000000000
875699961