#include using namespace std; #define int long long const int M = 1e9 + 7; using state = tuple; enum { ind, ful, ex3, md3, md8 }; state add(const state &a, const int &b, string &s) { return {get(a) + 1, get(a) && (b + '0' == s[get(a)]), get(a) || (b == 3), (get(a) * 10 + b) % 3, (get(a) * 10 + b) % 8}; } int mp[] = {3, 13, 23, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 43, 53, 63, 73, 83, 93}; int fun(int n, int k) { int ret = 0; for (int i = 1; i <= n; i++) if (i % 3 == k % 3) ret++; else if (binary_search(mp, mp + 19, i)) ret++; return ret; } int mem[200000][2][2][3][8]; int dp(string s, int lvl) { memset(mem, 0, sizeof(mem)); int n = s.size() - lvl, ldg = 0; for (int i = n; i < n + lvl; i++) ldg = (ldg * 10 + (s[i] - '0')); auto get_st = [&](state st) -> int & { return mem[get(st)][get(st)][get(st)][get(st)] [get(st)]; }; get_st({0, 1, 0, 0, 0}) = 1; for (int i = 0; i < n; i++) for (int f = 0; f < 2; f++) for (int e3 = 0; e3 < 2; e3++) for (int m3 = 0; m3 < 3; m3++) for (int m8 = 0; m8 < 8; m8++) for (int nxt = 0; nxt < 10; nxt++) { if (f && nxt > s[i] - '0') break; get_st(add({i, f, e3, m3, m8}, nxt, s)) += get_st({i, f, e3, m3, m8}); get_st(add({i, f, e3, m3, m8}, nxt, s)) %= M; } int ret = 0; for (int f = 0; f < 2; f++) for (int e3 = 0; e3 < 2; e3++) for (int m3 = 0; m3 < 3; m3++) if ((e3 || (m3 == 0))) for (int m8 = 1; m8 < 8; m8++) ret += get_st(state{n, f, e3, m3, m8}), ret %= M; for (int m3 = 0; m3 < 3; m3++) for (int m8 = 0; m8 < 8; m8++) ret += get_st(state{n, 0, 0, m3, m8}) * fun((int)pow(10, lvl) - 1, 3 - m3) % M + get_st(state{n, 1, 0, m3, m8}) * fun(ldg, 3 - m3) % M + get_st(state{n, 0, 1, m3, m8}) * ((int)pow(10, lvl) - 1) % M + get_st(state{n, 1, 1, m3, m8}) * ldg % M, ret %= M; return ret; } string minus1(string s) { for (int i = s.size() - 1; i >= 0; i--) { if (s[i] == '0') s[i] = '9'; else { s[i]--; break; } } return s; } signed main() { freopen("vr.in", "r", stdin); freopen("vr.out", "w", stdout); string A, B; int p; cin >> A >> B >> p; cout << (dp(B, floor(log10(p))) - dp(minus1(A), floor(log10(p))) + M) % M << endl; }