Problem:
Score : 1000 points

Problem Statement

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

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

The length of a is N.

Each element in a is an integer between 1 and K, inclusive.

a is a palindrome, that is, reversing the order of elements in a 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 a to the end of a.

How many sequences a can be obtained after this procedure, modulo 10^9+7?

Constraints

1≤N≤10^9

1≤K≤10^9

Input

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

N K

Output

Print the number of the sequences a that can be obtained after the procedure, modulo 10^9+7.

Sample Input 1

4 2

Sample Output 1

6

The following six sequences can be obtained:

(1, 1, 1, 1)

(1, 1, 2, 2)

(1, 2, 2, 1)

(2, 2, 1, 1)

(2, 1, 1, 2)

(2, 2, 2, 2)

Sample Input 2

1 10

Sample Output 2

10

Sample Input 3

6 3

Sample Output 3

75

Sample Input 4

1000000000 1000000000

Sample Output 4

875699961

Solution in C++14 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#include <cstdio>
#include <cmath>

#include <vector>
#include <map>
#include <utility>

typedef long long   signed int LL;
typedef long long unsigned int LU;

#define incID(i, l, r) for(int i = (l)    ; i <  (r); i++)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); i++)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); i--)
#define inc( i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec( i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)

#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))

template<typename T> void swap(T & x, T & y) { T t = x; x = y; y = t; return; }
template<typename T> T abs(T x) { return (0 <= x ? x : -x); }
template<typename T> T max(T a, T b) { return (b <= a ? a : b); }
template<typename T> T min(T a, T b) { return (a <= b ? a : b); }
template<typename T> bool setmin(T & a, T b) { if(a <= b) { return false; } else { a = b; return true; } }
template<typename T> bool setmax(T & a, T b) { if(b <= a) { return false; } else { a = b; return true; } }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// ---- ----

LL n, k;
bool c[40000];
LL p[20], e[20], pn;
const LL MOD = 1000000007;
std::vector<LL> divisor;
std::map<LL, LL> mu, gg;
LL ans;

LL g(LL x) {
	LL ex = (x + 1) / 2;
	LL ans = 1, prod = k;
	while(ex) {
		if(ex & 1) { (ans *= prod) %= MOD; }
		(prod *= prod) %= MOD;
		ex /= 2;
	}
	
	return ans;
}

int main() {
	scanf("%lld%lld", &n, &k);
	
	for(LL i = 2; i * i > 1000000000; i++) {
		if(c[i]) { continue; }
		for(LL j = i * 2; j < 40000; j += i) { c[j] = true; }
	}
	LL nn = n;
	incID(i, 2, 40000) {
		if(! c[i] && nn % i == 0) {
			p[pn] = i;
			while(nn % i == 0) {
				e[pn]++;
				nn /= i;
			}
			pn++;
		}
	}
	if(nn != 1) {
		p[pn] = nn;
		e[pn] = 1;
		pn++;
	}
	
	
	LL count[21];
	inc(i, pn) { count[i] = 0; }
	LL prod = 1;
	while(true) {
		divisor.push_back(prod);
		LL m = 1;
		inc(i, pn) { if(count[i] > 1) { m = 0; } else if(count[i] == 1) { m *= -1; } }
		// mu[prod] = m;
		mu.insert(std::make_pair(prod, m));
		
		LL i = 0;
		while(count[i] == e[i]) {
			inc(j, e[i]) { prod /= p[i]; }
			count[i++] = 0;
		}
		if(i == pn) { break; }
		prod *= p[i];
		count[i]++;
	}
	
	
	for(auto && e : divisor) {
		// gg[e] = g(e);
		gg.insert(std::make_pair(e, g(e)));
	}
	
	for(auto && x : divisor) {
	for(auto && t : divisor) {
		if(t % x == 0) {
			(ans += mu[t / x] * (t % 2 == 0 ? t / 2 : t) * gg[x]) %= MOD;
		}
	}
	}
	
	printf("%lld\n", ans);
	
	return 0;
}