#include using namespace std; typedef long long ll; const int MOD = 1e9 + 7; const int MAXLOG = 2e5 + 5; const int MAXP = 800; // P could be 8, 80, 800 #define rep( i, a ) for(int i = 0; i < ( a ); ++i) string a, b; int p; ll wayn_5[MAXLOG][2][2][3]; // first n - 5 digits: pos, less, has three, % 3 ll way5[6][2][2][3][800]; // last 5 digits: pos, less, has three, % 3, % P ll dp(const string &s){ memset( wayn_5, 0, sizeof(wayn_5) ); memset( way5, 0, sizeof(way5) ); wayn_5[0][0][0][0] = 1LL; int z = max( 0, s.size() - 5 ); rep( i, z ) rep( j, 2 ) rep( k, 2 ) rep( l, 3 ){ int lim = j ? 9 : s[ i ] - '0'; rep( d, lim + 1 ) ( wayn_5[i + 1][j || d < lim][k || d == 3][( l + d ) % 3] += wayn_5[i][j][k][l] ) %= MOD; } rep( j, 2 ) rep( k, 2 ) rep( l, 3 ) // move things to the important part way5[0][j][k][l][0] = wayn_5[z][j][k][l]; // % P = 0 because not yet determined int _z = s.size() - z; // last <= 5 digits that are crucial to mod P rep( i, _z ) rep( j, 2 ) rep( k, 2 ) rep( l, 3 ) rep( m, p ){ int lim = j ? 9 : s[ z + i ] - '0'; rep( d, lim + 1 ) ( way5[i + 1][j || d < lim][k || d == 3 ][( l + d ) % 3][( m * 10 + d ) % p] += way5[i][j][k][l][m] ) %= MOD; } ll ans = 0LL; rep( j, 2 ) rep( k, 2 ) rep( l, 3 ) rep( m, p ){ if( ( k || l == 0 ) && m != 0 ) ( ans += way5[_z][j][k][l][m] ) %= MOD; } return ans; } int digCnt(int x){ int res = 0; while( x > 0 ) x /= 10, ++res; return res; } bool match(const string &s){ int dsum = 0, has3 = 0; rep( i, s.size() ){ dsum += s[ i ] - '0'; if( s[ i ] == '3' ) has3 = 1; } if( dsum % 3 != 0 && !has3 ) return false; int x = digCnt( p ); if( s.size() < x ) return true; dsum = 0; for(int i = (int)s.size() - 2 - x; i < (int)s.size(); ++i){ if( i < 0 ) continue; dsum = dsum * 10 + s[ i ] - '0'; } return dsum % p != 0; } void solve(){ ll ans = dp( b ) - dp( a ); if( match( a ) ) ++ans; ans %= MOD; ans += MOD; ans %= MOD; cout << ans << endl; } int main(){ ios::sync_with_stdio(false); cin >> a >> b >> p; solve(); return 0; }