#include #define GET_MACRO(a, b, c, NAME, ...) NAME #define rep(...) GET_MACRO(__VA_ARGS__, rep3, rep2)(__VA_ARGS__) #define rep2(i, a) rep3 (i, 0, a) #define rep3(i, a, b) for (int i = (a); i < (b); i++) #define repr(...) GET_MACRO(__VA_ARGS__, repr3, repr2)(__VA_ARGS__) #define repr2(i, a) repr3 (i, 0, a) #define repr3(i, a, b) for (int i = (b) - 1; i >= (a); i--) template inline bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); } template inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } using namespace std; typedef long long ll; const ll mod = 1e9 + 7; ll dp[202020][2][3][2]; // pos, less, mod 3, three ll dp2[8][2][3][2][800]; // pos, less, mod 3, three, mod P ll f(string N, int P, bool less) { memset(dp, 0, sizeof(dp)); memset(dp2, 0, sizeof(dp2)); int s = max(0, N.length() - 5); int t = N.length() - s; dp[0][0][0][0] = 1; rep (i, s) rep (j, 2) rep (k, 3) rep (l, 2) { int lim = j ? 9 : N[i] - '0'; rep (x, lim + 1) { (dp[i + 1][j || x < lim][(k + x) % 3][l || x == 3] += dp[i][j][k][l]) %= mod; } } rep (j, 2) rep (k, 3) rep (l, 2) { dp2[0][j][k][l][0] = dp[s][j][k][l]; } rep (i, t) rep (j, 2) rep (k, 3) rep (l, 2) rep (m, P) { int lim = j ? 9 : N[s + i] - '0'; rep (x, lim + 1) { (dp2[i + 1][j || x < lim][(k + x) % 3][l || x == 3][(m * 10 + x) % P] += dp2[i][j][k][l][m]) %= mod; } } ll res = 0; rep (j, 2) rep (k, 3) rep (l, 2) rep (m, P) { if (less && j == 0) continue; if ((k == 0 || l) && m != 0) { (res += dp2[t][j][k][l][m]) %= mod; } } return res; } int main() { string A, B; int P; cin >> A >> B >> P; ll ans = f(B, P, false); ans -= f(A, P, true); ans %= mod; ans += mod; ans %= mod; cout << ans << endl; return 0; }