#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vll = vector<ll, ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
ll modpow(ll a, ll b, ll mod = (ll)(1e9 + 7)) {
  if (!b) return 1;
  a %= mod;
  return modpow(a * a % mod, b / 2, mod) * (b & 1 ? a : 1) % mod;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int DEBUG = 1;
const int mxn = 2007, mod = 1e9 + 7;
int n, k;
ll p[mxn][mxn];
int __gcd(int a, int b) { return a ? __gcd(b % a, a) : b; }
void add(ll &a, ll b) {
  a += b;
  if (a >= mod) a -= mod;
}
void sub(ll &a, ll b) {
  a -= b;
  if (a < 0) a += mod;
}
ll mul(ll a, ll b) { return a * b % mod; }
ll inv(ll a) { return modpow(a, mod - 2); }
int next(int x, int n) {
  x = x % n;
  if (!x) return -1;
  return (x + k - 1) % (n - 1);
}
ll half = inv(2);
ll fi(int n, int x) {
  if (x == -1) return 0;
  if (n == 1) return 1;
  if (p[n][x] != -1) return p[n][x];
  int g = __gcd(n, k);
  ll X = 0, cf = 1;
  for (int m = 0; m <= n / g - 1; m++) {
    cf = mul(cf, half);
    add(X, mul(cf, fi(n - 1, next(x + 1LL * m * (k % n), n))));
  }
  ll divi = inv((1 + mod - cf) % mod);
  ll idivi = (1 + mod - cf) % mod;
  int y = x;
  do {
    p[n][y] = mul(X, divi);
    X = mul(2, X);
    sub(X, mul(idivi, fi(n - 1, next(y, n))));
    y = (y + k) % n;
  } while (y != x);
  return p[n][x];
}
void solve() {
  for (int i = 0; i < mxn; i++)
    for (int j = 0; j < mxn; j++) p[i][j] = -1;
  cout << fi(n, (k - 1) % n) << endl;
}
struct BearCircleGame {
  int winProbability(int a, int b) {
    n = a;
    k = b;
    for (int i = 0; i < mxn; i++)
      for (int j = 0; j < mxn; j++) p[i][j] = -1;
    return fi(n, (k - 1) % n);
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  while (cin >> n >> k) solve();
  return 0;
}
