N= int(input())

import sys
sys.setrecursionlimit(10000)

bit = bin(N)[2:]

dp = [[0] * 9 for i in range(len(bit) + 1)]

def aaa(bit, k):
    
    res = 0
    for i in range(9):
        a = i // 3
        b = i % 3
        if (int(bit[0]) -b) + (k -a) *2 >= 2:
            res += 3**(len(bit) - 1) % 1000000007
        elif (int(bit[0]) -b) + (k -a) *2 < 0:
            pass
        else:
            if len(bit) > 1:
                res += aaa(bit[1:], (int(bit[0]) -b) + (k -a) *2)
            else:
                res += 1
    print(bit, k, res)
    return res

print(aaa(bit, 0))
    
        
