import java.util.*; public class Main { static final int MOD = 1000000007; public static void main (String[] args) { Scanner sc = new Scanner(System.in); char[] arr = sc.next().toCharArray(); int m = sc.nextInt(); TreeMap map = new TreeMap<>(); TreeMap next = new TreeMap(); map.put(0, 1); boolean hasZero = false; for (int i = 0; i < arr.length; i++) { int x = arr[i] - '0'; if (x == 0) { hasZero = true; } while (map.size() > 0) { Map.Entry entry = map.pollFirstEntry(); int y = entry.getKey(); if (next.containsKey(y)) { next.put(y, (entry.getValue() + next.get(y)) % MOD); } else { next.put(y, entry.getValue()); } int z; if (x == 0 && y == 0) { z = entry.getValue() - 1; } else { z = entry.getValue(); } y = (x + y) % m; if (next.containsKey(y)) { next.put(y, (z + next.get(y)) % MOD); } else { next.put(y, z); } } TreeMap tmp = next; next = map; map = tmp; } int ans = map.get(0); if (!hasZero) { ans--; } System.out.println(ans); } }