#!/usr/bin/ruby Mod=10**9+7 def count(i,d) (d+1).pow(i)-1 end def sum(i,d) ((d+1)*((d+1).pow(i,Mod)-1)%Mod*d.pow(Mod-2,Mod)%Mod-i+Mod)%Mod end def sum_n(i,d,n) return 0 if n==0 s=count(i-1,d) t=n/(1+s) (t*(i+sum(i-1,d))%Mod+sum_n(i-1,d,n-t*(1+s)))%Mod end n,b,d=`dd`.split.map &:to_i i=1 loop{ if count(i,d)>n p (sum(b,d)+Mod-sum_n(i,d,n))%Mod exit end i+=1 }