Problem: Assume that sk(n) equals the sum of digits of number n in the k-based notation. For example, s2(5) = s2(1012) = 1 + 0 + 1 = 2, s3(14) = s3(1123) = 1 + 1 + 2 = 4.

The sequence of integers a0, ..., an - 1 is defined as <image>. Your task is to calculate the number of distinct subsequences of sequence a0, ..., an - 1. Calculate the answer modulo 109 + 7.

Sequence a1, ..., ak is called to be a subsequence of sequence b1, ..., bl, if there is a sequence of indices 1 ≤ i1 < ... < ik ≤ l, such that a1 = bi1, ..., ak = bik. In particular, an empty sequence (i.e. the sequence consisting of zero elements) is a subsequence of any sequence.

Input

The first line contains two space-separated numbers n and k (1 ≤ n ≤ 1018, 2 ≤ k ≤ 30).

Output

In a single line print the answer to the problem modulo 109 + 7.

Examples

Input

4 2


Output

11


Input

7 7


Output

128

Note

In the first sample the sequence ai looks as follows: (0, 1, 1, 0). All the possible subsequences are: 

(), (0), (0, 0), (0, 1), (0, 1, 0), (0, 1, 1), (0, 1, 1, 0), (1), (1, 0), (1, 1), (1, 1, 0).

In the second sample the sequence ai looks as follows: (0, 1, 2, 3, 4, 5, 6). The subsequences of this sequence are exactly all increasing sequences formed from numbers from 0 to 6. It is easy to see that there are 27 = 128 such sequences.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
template <typename T>
struct Matrix {
  int h, w;
  vector<T> a;
  Matrix(int n_ = 0) : h(n_), w(n_), a(n_ * n_) {}
  Matrix(int n_, int m_) : h(n_), w(m_), a(n_ * m_) {}
  Matrix(int n_, int m_, vector<T> a_) : h(n_), w(m_), a(a_) {}
  T& get(int r, int c) { return a[r * w + c]; }
  T get(int r, int c) const { return a[r * w + c]; }
  Matrix& operator+=(const Matrix& m) {
    assert(h == m.h && w == m.w);
    for (int i = 0; i < w * h; ++i) a[i] += m.a[i];
  }
  Matrix& operator-=(const Matrix& m) {
    assert(h == m.h && w == m.w);
    for (int i = 0; i < w * h; ++i) a[i] -= m.a[i];
  }
  Matrix& operator*=(const Matrix& m) {
    assert(w == m.h);
    Matrix ret(h, m.w);
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < m.w; ++j) {
        for (int k = 0; k < w; ++k) {
          ret.a[i * m.w + j] += a[i * w + k] * m.a[k * m.w + j] % mod;
        }
        ret.a[i * m.w + j] %= mod;
      }
    }
    return (*this) = ret;
  }
  Matrix operator+(const Matrix& m) const { return Matrix(*this) += m; }
  Matrix operator-(const Matrix& m) const { return Matrix(*this) -= m; }
  Matrix operator*(const Matrix& m) const { return Matrix(*this) *= m; }
};
template <typename T>
Matrix<T> unit(int n) {
  Matrix<T> ret(n);
  for (int i = (int)(0); i < (int)(n); ++i) ret.get(i, i) = 1;
  return ret;
}
template <typename T>
Matrix<T> matpow(Matrix<T> a, int n) {
  assert(a.h == a.w);
  Matrix<T> ret = unit<T>(a.h);
  while (n) {
    if (n & 1) ret *= a;
    a *= a;
    n >>= 1;
  }
  return ret;
}
Matrix<ll> a[65];
Matrix<ll> calc(int i, int k) {
  return matpow(a[i + k], k - i) * matpow(a[i], i);
}
ll solve(ll n, ll k) {
  ll cnt = 0;
  ll tmp = n;
  while (tmp) {
    tmp /= k;
    ++cnt;
  }
  Matrix<ll> b[cnt + 5], c[cnt + 5], d[k + 1];
  b[0] = a[k - 1];
  c[0] = matpow(b[0], k - 1);
  b[1] = calc(k - 1, k);
  c[1] = matpow(b[1], k - 1);
  Matrix<ll> s = unit<ll>(2 * k + 1);
  for (int j = (int)(2); j < (int)(cnt + 2); ++j) {
    b[j] = calc(k - j % k, k);
    s *= c[j - 1];
    b[j] *= s;
    c[j] = matpow(b[j], k - 1);
  }
  int idx = 1;
  int rem = n % k;
  n -= rem;
  int q = 0;
  tmp = n;
  if (tmp) {
    while (tmp % k == 0) {
      ++q;
      tmp /= k;
    }
  }
  q = (k - q % k) % k;
  Matrix<ll> ans = unit<ll>(2 * k + 1);
  ans = matpow(a[q], rem) * matpow(a[q + k], max(rem - q, 0));
  n /= k;
  if (!n) {
    return (ans.get(2 * k, 0) + ans.get(0, 0)) % mod;
  }
  --n;
  while (n) {
    ll t = n % k;
    if (t) {
      ans *= matpow(b[idx], t);
    }
    n /= k;
    ++idx;
  }
  ans *= b[1];
  return (ans.get(2 * k, 0) + ans.get(0, 0)) % mod;
}
int main() {
  ll n, k;
  cin >> n >> k;
  for (int i = (int)(0); i < (int)(2 * k + 1); ++i) {
    a[i] = Matrix<ll>(2 * k + 1);
    for (int j = (int)(0); j < (int)(i); ++j) a[i].get(0, j) = 1;
    for (int j = (int)(0); j < (int)(2 * k - 1); ++j) a[i].get(j + 1, j) = 1;
    a[i].get(2 * k, 0) = 1;
    a[i].get(2 * k, 2 * k) = 1;
  }
  cout << solve(n, k) << endl;
  return 0;
}