import java.util.*; public class Main { public static long MOD = (long)Math.pow(10, 9) + 7; public static void main(String[] args) { Scanner sc = new Scanner(System.in); String A = sc.next(); String B = sc.next(); int p = sc.nextInt(); long ans = (func(B, p) - func(A, p) + MOD) % MOD; int m1 = 0; int kosuu = 0; int m2 = 0; for(int i = 0; i < A.length(); i++) { int d = Integer.parseInt(String.valueOf(A.charAt(i))); if(d == 3) kosuu++; m1 = (m1 + d) % 3; m2 = (10 * m2 + d) % p; } if(((m1 == 0) || (kosuu > 0)) && (m2 != 0)) ans = (ans + 1) % MOD; System.out.println(ans); } public static long func(String X, int p) { int len = X.length(); long[][][][][] dp = new long[len][3][2][p][2]; int first = Integer.parseInt(String.valueOf(X.charAt(0))); dp[0][0][0][0][1]++; if(first == 3) { dp[0][(first) % 3][1][(first) % p][0]++; } else { dp[0][(first) % 3][0][(first) % p][0]++; } for(int i = 1; i < first; i++) { if(i == 3) { dp[0][i % 3][1][i % p][1]++; } else { dp[0][i % 3][0][i % p][1]++; } } for(int i = 1; i < len; i++) { int d = Integer.parseInt(String.valueOf(X.charAt(i))); for(int j = 0; j < 3; j++) { for(int k = 0; k < p; k++) { for(int l = 0; l <= d; l++) { if(l == d) { if(l == 3) { dp[i][(j + l) % 3][1][(10 * k + l) % p][0] = (dp[i][(j + l) % 3][1][(10 * k + l) % p][0] + dp[i - 1][j][0][k][0]) % MOD; } else { dp[i][(j + l) % 3][0][(10 * k + l) % p][0] = (dp[i][(j + l) % 3][0][(10 * k + l) % p][0] + dp[i - 1][j][0][k][0]) % MOD; } } else { if(l == 3) { dp[i][(j + l) % 3][1][(10 * k + l) % p][1] = (dp[i][(j + l) % 3][1][(10 * k + l) % p][1] + dp[i - 1][j][0][k][0]) % MOD; } else { dp[i][(j + l) % 3][0][(10 * k + l) % p][1] = (dp[i][(j + l) % 3][0][(10 * k + l) % p][1] + dp[i - 1][j][0][k][0]) % MOD; } } } for(int l = 0; l <= 9; l++) { if(l == 3) { dp[i][(j + l) % 3][1][(10 * k + l) % p][1] = (dp[i][(j + l) % 3][1][(10 * k + l) % p][1] + dp[i - 1][j][0][k][1]) % MOD; } else { dp[i][(j + l) % 3][0][(10 * k + l) % p][1] = (dp[i][(j + l) % 3][0][(10 * k + l) % p][1] + dp[i - 1][j][0][k][1]) % MOD; } } for(int l = 0; l <= d; l++) { if(l == d) { dp[i][(j + l) % 3][1][(10 * k + l) % p][0] = (dp[i][(j + l) % 3][1][(10 * k + l) % p][0] + dp[i - 1][j][1][k][0]) % MOD; } else { dp[i][(j + l) % 3][1][(10 * k + l) % p][1] = (dp[i][(j + l) % 3][1][(10 * k + l) % p][1] + dp[i - 1][j][1][k][0]) % MOD; } } for(int l = 0; l <= 9; l++) { dp[i][(j + l) % 3][1][(10 * k + l) % p][1] = (dp[i][(j + l) % 3][1][(10 * k + l) % p][1] + dp[i - 1][j][1][k][1]) % MOD; } } } } long ret = 0; for(int k = 1; k < p; k++) { for(int l = 0; l < 2; l++) { ret = (ret + dp[len - 1][0][0][k][l]) % MOD; } } for(int k = 1; k < p; k++) { for(int l = 0; l < 2; l++) { for(int j = 0; j < 3; j++) { ret = (ret + dp[len - 1][j][1][k][l]) % MOD; } } } return ret; } }