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>
using namespace std;
const int maxn = 30 + 2;
struct mat {
  int n, m, a[maxn][maxn];
  mat() {}
  mat(int _n, int _m) {
    n = _n;
    m = _m;
    memset(a, 0, sizeof(a));
  }
};
mat operator*(const mat &p, const mat &q) {
  mat r(p.n, q.m);
  for (int i = 0; i < p.n; i++)
    for (int j = 0; j < q.m; j++)
      for (int z = 0; z < p.m; z++)
        r.a[i][j] += (long long)p.a[i][z] * q.a[z][j] % 1000000007,
            r.a[i][j] %= 1000000007;
  return r;
}
mat M[61][maxn], I[61][maxn];
int k;
long long n;
void cal_mat() {
  for (int i = 0; i < k; i++) {
    M[0][i].n = M[0][i].m = k + 1;
    I[0][i].n = I[0][i].m = k + 1;
    for (int j = 0; j <= k; j++) M[0][i].a[j][j] = I[0][i].a[j][j] = 1;
    for (int j = 0; j <= k; j++)
      if (j != i) M[0][i].a[i][j] = 1, I[0][i].a[i][j] = 1000000007 - 1;
  }
  int cnt = 0;
  for (long long i = n; i; i /= k, cnt++)
    ;
  for (int i = 1; i < cnt; i++) {
    M[i][0] = M[i - 1][0];
    I[i][0] = I[i - 1][0];
    for (int j = 1; j < k; j++)
      M[i][0] = M[i][0] * M[i - 1][j], I[i][0] = I[i - 1][j] * I[i][0];
    for (int j = 1; j < k; j++) {
      M[i][j] = I[i - 1][j - 1] * M[i][j - 1] * M[i - 1][j - 1];
      I[i][j] = I[i - 1][j - 1] * I[i][j - 1] * M[i - 1][j - 1];
    }
  }
}
int cal(long long num) { return num ? (num % k + cal(num / k)) % k : 0; }
long long pw[61];
int a[61];
int main() {
  scanf("%I64d%d", &n, &k);
  cal_mat();
  int cnt = 0;
  for (; n; n /= k, cnt++) a[cnt] = n % k;
  for (int i = 0; i < cnt; i++) pw[i] = (i ? pw[i - 1] * k : 1);
  mat p(k + 1, k + 1);
  for (int i = 0; i <= k; i++) p.a[i][i] = 1;
  mat q(k + 1, 1);
  q.a[k][0] = 1;
  long long now = 0LL;
  for (int i = cnt - 1; i >= 0; i--)
    if (a[i]) {
      int x = cal(now);
      for (int j = 0; j < a[i]; j++) p = p * M[i][(x + j) % k];
      now += a[i] * pw[i];
    }
  q = p * q;
  int ans = 0;
  for (int i = 0; i <= k; i++) {
    ans = ans + q.a[i][0];
    if (ans >= 1000000007) ans -= 1000000007;
  }
  printf("%d\n", ans);
}