#include #define REP(i, a, n) for (ll i = ((ll) a); i < ((ll) n); i++) using namespace std; typedef long long ll; const ll MOD = 1000000007; int main(void) { ll N; cin >> N; vector S; for (ll n = N; n > 0; n /= 2) S.push_back(n % 2); reverse(S.begin(), S.end()); { ll x = 0; REP(i, 0, S.size()) x = x * 2 + S[i]; assert(x == N); } #define update(x, y, c, d) (((x) = (((x) + ((y) * 2 % MOD)) % MOD + ((c) * (d)) % MOD) % MOD)) % MOD vector>> dp(S.size() + 1, vector>(S.size() + 1, vector(2, 0))); vector>> cnt(S.size() + 1, vector>(S.size() + 1, vector(2, 0))); vector>> ok(S.size() + 1, vector>(S.size() + 1, vector(2, false))); cnt[0][0][1] = 1; ok[0][0][1] = true; REP(i, 0, S.size()) REP(j, 0, S.size() + 1) { if (ok[i][j][0]) { update(dp[i + 1][j][0], dp[i][j][0], cnt[i][j][0], 0); cnt[i + 1][j][0] += cnt[i][j][0]; ok[i + 1][j][0] = true; } if (ok[i][j][0] && j + 1 <= S.size()) { update(dp[i + 1][j + 1][0], dp[i][j][0], cnt[i][j][0], 1); cnt[i + 1][j + 1][0] += cnt[i][j][0]; ok[i + 1][j + 1][0] = true; } if (ok[i][j][1]) { update(dp[i + 1][j][S[i] == 0], dp[i][j][1], cnt[i][j][1], 0); cnt[i + 1][j][S[i] == 0] += cnt[i][j][1]; ok[i + 1][j][S[i] == 0] = true; } if (ok[i][j][1] && j + 1 <= S.size() && S[i] == 1) { update(dp[i + 1][j + 1][1], dp[i][j][1], cnt[i][j][1], 1); cnt[i + 1][j + 1][1] += cnt[i][j][1]; ok[i + 1][j + 1][1] = true; } } ll ans = 0; REP(i, 0, S.size() + 1) ans = (ans + (dp[S.size()][i][0] * i) % MOD) % MOD; REP(i, 0, S.size() + 1) ans = (ans + (dp[S.size()][i][1] * i) % MOD) % MOD; cout << ans << endl; }