#include <cstdio>
#include <cstdint>
#include <vector>
#include <algorithm>
#include <utility>
#include <numeric>
#include <map>

constexpr intmax_t  operator ""_jd(unsigned long long n) { return n; }
constexpr uintmax_t operator ""_ju(unsigned long long n) { return n; }
constexpr size_t    operator ""_zu(unsigned long long n) { return n; }
// constexpr ptrdiff_t operator ""_td(unsigned long long n) { return n; }

std::vector<intmax_t> divisor(intmax_t n) {
  std::vector<intmax_t> res;
  for (intmax_t i = 1; i*i <= n; ++i) {
    if (n % i) continue;
    res.push_back(i);
    if (n/i != i) res.push_back(n/i);
  }
  std::sort(res.begin(), res.end());
  return res;
}

std::vector<intmax_t> factor(intmax_t n) {
  std::vector<intmax_t> res;
  for (intmax_t i = 2; i*i <= n; ++i) {
    if (n % i) continue;
    res.push_back(i);
    do n /= i; while (n % i == 0);
  }
  if (n > 1) res.insert(std::lower_bound(res.begin(), res.end(), n), n);
  return res;
}

std::map<intmax_t, intmax_t> moebius(intmax_t n) {
  auto fs = factor(n);
  size_t m = fs.size();
  std::map<intmax_t, intmax_t> res;
  for (size_t i = 0; i < (1_zu << m); ++i) {
    intmax_t d = 1;
    intmax_t mu = 1;
    for (size_t j = 0; j < m; ++j) {
      if (i >> j & 1) {
        d *= fs[j];
        mu = -mu;
      }
    }
    res[d] = mu;
  }
  return res;
}

template <typename InputIt>
auto compress(InputIt first, InputIt last) {
  using value_type = typename InputIt::value_type;
  std::map<value_type, size_t> res;
  while (first != last) res[*first++];
  size_t i = 0;
  for (auto& p: res) p.second = i++;
  return res;
}

template <typename Tp>
Tp gcd(Tp a, Tp b, Tp& x, Tp& y) {
  x = Tp(0);
  y = Tp(1);
  for (Tp u = y, v = x; a;) {
    Tp q = b/a;
    std::swap(x -= q*u, u);
    std::swap(y -= q*v, v);
    std::swap(b -= q*a, a);
  }
  return b;
}

template <typename Tp>
Tp modinv(Tp a, Tp mod) {
  Tp x, y;
  gcd(a, mod, x, y);
  x %= mod;
  if (x < 0) x += mod;
  return x;
}

template <typename Tp>
Tp modadd(Tp a, Tp b, Tp mod) {
  a += b % mod;
  if (a < 0) a += mod;
  if (a >= mod) a -= mod;
  return a;
}

template <typename Tp>
Tp modadd(std::initializer_list<Tp> const& adds, Tp mod) {
  Tp res = 0;
  for (auto const& add: adds) {
    res += add % mod;
    if (res < 0) res += mod;
    if (res >= mod) res -= mod;
  }
  return res;
}

template <typename Tp>
Tp modsub(Tp a, Tp b, Tp mod) {
  a -= b % mod;
  if (a < 0) a += mod;
  if (a >= mod) a -= mod;
  return a;
}

template <typename Tp>
Tp modmul(std::initializer_list<Tp> const& muls, Tp mod) {
  Tp res = 1;
  for (auto const& mul: muls) (res *= mul) %= mod;
  return res;
}

template <typename Tp>
Tp modpow(Tp base, intmax_t iexp, Tp mod) {
  Tp res = 1;
  for (Tp dbl = base; iexp; iexp >>= 1) {
    if (iexp & 1) res = res * dbl % mod;
    dbl = dbl * dbl % mod;
  }
  return res;
}

constexpr intmax_t mod = 1e9+7;

int main() {
  intmax_t n, k;
  scanf("%jd %jd", &n, &k);

  std::vector<intmax_t> ds = divisor(n);
  std::vector<intmax_t> f(ds.size());
  for (size_t i = 0; i < ds.size(); ++i)
    f[i] = modpow(k, (ds[i]+1)/2, mod);

  std::map<intmax_t, intmax_t> mu = moebius(n);
  std::vector<size_t> dp(ds.size());
  for (size_t i = 0; i < ds.size(); ++i) {
    for (size_t j = 0; j <= i; ++j) {
      if (ds[i] % ds[j]) continue;
      intmax_t cur = (mu[ds[i]/ds[j]] * f[j]) % mod;
      if (cur < 0) cur += mod;
      (dp[i] += cur) %= mod;
    }
  }

  intmax_t inv2 = modpow(2_jd, mod-2, mod);
  intmax_t res = 0;
  for (size_t i = 0; i < dp.size(); ++i) {
    intmax_t cur = dp[i];
    (cur *= ds[i]) %= mod;
    if (ds[i] % 2 == 0) (cur *= inv2) %= mod;
    (res += cur) %= mod;
  }
  printf("%jd\n", res);
}


Measured memory_bytes: 384