MOD = 10**9 + 7

def count_numbers(R_str, K):
    def dp(pos, ones_mod_K, tight):
        nonlocal memo
        key = (pos, ones_mod_K, tight)
        if pos == len(binary_R):
            return 1 if ones_mod_K == 0 else 0
        
        if key in memo:
            return memo[key]
        
        limit = binary_R[pos] if tight else 1
        res = 0
        
        for digit in range(limit + 1):
            new_tight = tight and (digit == limit)
            new_ones_mod_K = (ones_mod_K + digit) % K
            res = (res + dp(pos + 1, new_ones_mod_K, new_tight)) % MOD
        
        memo[key] = res
        return res

    # Конвертируем R в двоичное представление
    decimal_R = int(R_str)
    binary_R = bin(decimal_R)[2:]
    memo = {}
    
    # Начинаем с позиции 0, кол-ва единиц 0 и флага true (строгое совпадение с R)
    ans = dp(0, 0, True)
    return ans

# Чтение входных данных
n, K = map(int, input().split())
R = input().strip()

# Вывод результата
print(count_numbers(R, K))