#include<iostream>
#include<map>
using namespace std;
long long n;
map<long long, long long>M;
long long solve_1(long long W) {
	if (M[W] >= 1)return M[W];
	if (W == 0)return 0;
	if (W == 1)return 1;
	if (W % 2 == 0) {
		long long a1 = solve_1(W / 2);
		M[W] = a1;
		return a1;
	}
	if (W % 2 == 1) {
		long long a1 = solve_1(W / 2) + solve_1(W / 2 + 1);
		M[W] = a1;
		return a1;
	}
}
long long solve(long long W) {
	if (W == 0)return 0;
	if (W == 1)return 1;
	if (W % 2 == 0) { return (3 * solve(W / 2) - solve_1(W / 2) + 1000000007) % 1000000007; }
	if (W % 2 == 1) { return (3 * solve(W / 2) + solve_1(W / 2 + 1) + 1000000007) % 1000000007; }
}
int main() {
	cin >> n;
	cout << solve(n + 1) << endl;
	return 0;
}