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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		
		if(N%2==1){
			if(N==1){
				Console.WriteLine(K);
				return;
			}
			
			long ret = ModPow(K,(N+1)/2);
			ret += (mod - K);
			ret %= mod;
			
			ret *= N;
			ret %= mod;
			
			ret += K;
			ret %= mod;
			
			Console.WriteLine(ret);
			return;
		}
		
		long tot = ModPow(K,N/2);
		long per = ModPow(K,((N/2)+1)/2);
		long notp= (tot - per + mod) % mod;
		notp *= N;
		notp %= mod;
		
		per += (mod - K);
		per %= mod;
		
		per *= (N/2);
		per %= mod;
		
		long ans = (notp + per + K) %mod;
		Console.WriteLine(ans);
		
		
	}
	long N,K;
	public Sol(){
		var d = rla();
		N = d[0];
		K = d[1];
	}
	static long mod = (long)1e9+7;
	static long ModPow(long n,long k){
		
		if(k==0)return 1;
		if(n==0)return 0;
		long ret=1;
		long x=n;
		while(k>0){
			if((k&1)==1){ret*=x;ret%=mod;}
			
			x*=x;x%=mod;
			k>>=1;
		}
		return ret;
		
	}

	public static long ModInv(long n){
		//(mod-2)乗を返す
		// modが素数の時だけ!!!!
		return ModPow(n,mod-2);
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}