#include <cstdio>
#include <map>
const long long MOD = 1000000007;
long long N, O, pow3[61];
std::map < long long, long long > ans[61];
long long F(long long n, int w)
{
	if (w == -1)
		return 1;
	if (n >= (4LL << w) - 2)
		return pow3[w + 1];
	auto it = ans[w].find(n);
	if (it != ans[w].end())
		return it -> second;
	long long r = 0;
	for (long long i = 0; i <= 2 && i <= n >> w; i++)
		r += F(n - (i << w), w - 1);
	return ans[w][n] = r % MOD;
}
int main()
{
	scanf("%lld", &N);
	pow3[0] = 1;
	for (int i = 1; i <= 60; i++)
		pow3[i] = pow3[i - 1] * 3 % MOD;
	printf("%lld\n", F(N, 60));
	return 0;
}
