mod = 10 ** 9 + 7 # x=yのとき条件が成り立たないので # 大小関係無視して後で2で割ればOK N = int(input()) S = bin(N)[2:] L = len(S) dp = [[[[0] * 3 for _ in range(3)] for _ in range(2)] for _ in range(L + 1)] dp[0][0][0][0] = 1 for i, s in enumerate(S): nd = int(s) for ja in range(2): for jb in range(2): for k in range(3): for a in range(2): for b in range(2): ni = i + 1 nja = ja njb = jb nk = k if nja == 0: if nd > a: nja = 1 if nd < a: continue if njb == 0: if nd > b: njb = 1 if nd < b: continue if nk == 0: if a ^ b: nk += 1 if a & b: continue elif nk == 1: if a & b: nk += 1 dp[ni][nja][njb][nk] += dp[i][ja][jb][k] dp[ni][nja][njb][nk] %= mod ans = 0 for ja in range(2): for jb in range(2): ans += dp[L][ja][jb][2] ans %= mod print(ans * pow(2, mod - 2, mod) % mod)