#include<bits/stdc++.h>
const int md = 1e9+7;
long long n, a = 1, b, c;

int main(){
	scanf("%lld", &n);
	for(int i = 60; i >= 0; i--){
		((c *= 3) += b*(1+(n>>i&1))) %= md;
		(n>>i&1 ? b:a) = (b+a) % md;
	}
	printf("%lld\n", (a+b+c)%md);
}