A = int(input()) B = int(input()) B= int(str(B),A) i = 0 x = 1 j = A ans = 0 mod = 10 ** 9 + 7 while x * A <= B: ans = (ans + (A-1) * x * i) % mod i += 1 x *= A ans = (ans + (B - x + 1) * i) % mod print(ans)