A = int(input()) B = int(input()) B= int(str(B),A) x = 1 j = A ans = 0 mod = 10 ** 9 + 7 def check(x): return pow(A,x) <= B lb = 0 ub = 10 ** 5 + 10 while ub - lb > 1: mid = (ub + lb) // 2 if check(mid): lb = mid else: ub = mid for i in range(lb+1): ans = (ans + (A-1) * x * i) % mod x = (x * A) % mod ans = (ans + (B - x + 1) * i) % mod print(ans)