#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
 
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef long double ld; 
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;

const int MOD = 1e9 + 7;

map<ll,ll> ma;
ll b(ll n)
{
	if(n<2) return n;
	if(ma[n]!=0) return ma[n];
	if(n%2==0)
	{
		return (ma[n]=b(n/2));
	}
	else
	{
		ll x = n-1;
		ll y = n+1;
		while(x%2==0) x>>=1;
		while(y%2==0) y>>=1;
		return (ma[n]=b(x)+b(y));
	}
}

ll a(ll n)
{
	if(n==1) return 1;
	if(n==2) return 2;
	if(n&1)
	{
		ll k = (n-1)/2;
		return ((a(k)*3LL)%MOD + b(k+1))%MOD;
	}
	else
	{
		ll k = n/2;
		return ((a(k)*3LL)%MOD - b(k))%MOD;
	}
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	ll n; cin>>n;
	cout<<a(n+1);
}
