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>
constexpr int P = 1000000007;
using i64 = long long;
int norm(int x) {
  if (x < 0) {
    x += P;
  }
  if (x >= P) {
    x -= P;
  }
  return x;
}
template <class T>
T power(T a, int b) {
  T res = 1;
  for (; b; b /= 2, a *= a) {
    if (b % 2) {
      res *= a;
    }
  }
  return res;
}
struct Z {
  int x;
  Z(int x = 0) : x(norm(x)) {}
  int val() const { return x; }
  Z inv() const {
    assert(x != 0);
    return power(*this, P - 2);
  }
  Z &operator*=(const Z &rhs) {
    x = i64(x) * rhs.x % P;
    return *this;
  }
  Z &operator+=(const Z &rhs) {
    x = norm(x + rhs.x);
    return *this;
  }
  Z &operator-=(const Z &rhs) {
    x = norm(x - rhs.x);
    return *this;
  }
  Z &operator/=(const Z &rhs) { return *this *= rhs.inv(); }
  friend Z operator*(const Z &lhs, const Z &rhs) {
    Z res = lhs;
    res *= rhs;
    return res;
  }
  friend Z operator+(const Z &lhs, const Z &rhs) {
    Z res = lhs;
    res += rhs;
    return res;
  }
  friend Z operator-(const Z &lhs, const Z &rhs) {
    Z res = lhs;
    res -= rhs;
    return res;
  }
  friend Z operator/(const Z &lhs, const Z &rhs) {
    Z res = lhs;
    res /= rhs;
    return res;
  }
};
constexpr int K = 31;
struct Matrix {
  Z a[K][K];
  Matrix(int x = 0) : a{} {
    for (int i = 0; i < K; i++) {
      a[i][i] = x;
    }
  }
};
Matrix operator*(const Matrix &a, const Matrix &b) {
  Matrix res;
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < K; j++) {
      for (int k = 0; k < K; k++) {
        res.a[i][j] += a.a[i][k] * b.a[k][j];
      }
    }
  }
  return res;
}
Matrix shift(const Matrix &a, int d, int k) {
  Matrix res;
  res.a[k][k] = 1;
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < k; j++) {
      res.a[(i + d) % k][(j + d) % k] = a.a[i][j];
    }
    res.a[k][(i + d) % k] = a.a[k][i];
  }
  return res;
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  i64 n;
  int k;
  std::cin >> n >> k;
  Matrix ans(1);
  Matrix g(1);
  for (int i = 0; i <= k; i++) {
    g.a[i][0] = 1;
  }
  std::vector<int> d;
  while (n) {
    d.push_back(n % k);
    n /= k;
  }
  int m = d.size();
  int s = std::accumulate(d.begin(), d.end(), 0);
  for (int i = 0; i < m; i++) {
    s -= d[i];
    for (int j = d[i] - 1; j >= 0; j--) {
      ans = shift(g, j + s, k) * ans;
    }
    Matrix f(1);
    for (int j = 0; j < k; j++) {
      f = f * shift(g, j, k);
    }
    g = f;
  }
  Z sum;
  for (int i = 0; i <= k; i++) {
    sum += ans.a[k][i];
  }
  std::cout << sum.val() << "\n";
  return 0;
}