#include using namespace std; using ll = long long; const ll MOD = 1000000007; ll modpow(ll x, ll n, ll mod = MOD) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } ll f[70], fi[70]; void init(int sz) { f[0] = 1; for (int i = 1; i <= sz; i++) f[i] = f[i - 1] * i % MOD; fi[sz] = modpow(f[sz], MOD - 2); for (int i = sz; i > 0; i--) { fi[i - 1] = fi[i] * i % MOD; } } ll comb(ll r, ll c) { if (r < c) return 0; return f[r] * fi[c] % MOD * fi[r - c] % MOD; } ll calc1(int r, int c) { return comb(r, c) * c % MOD * modpow(r, MOD - 2) % MOD * (((1LL << r) - 1) % MOD) % MOD; } ll solve2(vector& v, int cur) { if (cur == v.size()) return 1; return (comb(v[cur], (int)v.size() - cur) + solve2(v, cur + 1)) % MOD; } ll solve1(vector& v, int cur) { if (cur == v.size()) return 0; return (calc1(v[cur], (int)v.size() - cur) + solve1(v, cur + 1) + solve2(v, cur + 1) * ((1LL << v[cur]) % MOD)) % MOD; } ll solve(ll n) { ll res = 0; for (ll i = 1; i <= n; i++) (res += i * __builtin_popcountll(i)) %= MOD; return res; } ll compute(ll n, int sz) { ll ans = 0; for (int i = 1; i <= sz; i++) { if ((1LL << i) - 1 > n) break; vector v; ll tmp = 0; int prv = sz; for (int j = 1; j <= i; j++) { int cur = i - j; while (cur < prv - 1) { ll hoge = tmp + (1LL << cur) + ((1LL << (i - j)) - 1); if (hoge > n) { cur--; break; } cur++; } ll hoge = tmp + (1LL << cur) + ((1LL << (i - j)) - 1); if (hoge > n) cur--; prv = cur; //cout << i << " " << cur << endl; v.push_back(cur); tmp += (1LL << cur); } (ans += solve1(v, 0) * i % MOD) %= MOD; //cout << tmp << endl; //cout << tmp << " " << solve1(v, 0) << endl; //cout << ans << endl; } return ans; } int main() { cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; int sz = 0; while (n >= (1LL << sz)) sz++; init(sz); ll ans = compute(n, sz); cout << ans << endl; return 0; }