#include #include #include #include #include #include #include using namespace std; using ll = long long; constexpr int P = 1000000007; int main() { ll n; cin >> n; n++; ll p[64] = {}; ll s = 1, t = 1; for (int k = 0; k < 61; k++) { p[k] = (s - t) % P; s *= 4; s %= P; t *= 3; t %= P; } ll r = 0; ll y = 0; for (int k = 0; k < 61; k++) { ll x = 1LL << k; if (x * 2 <= n) { r += p[k]; y = x * 2; } } for (int k = 0; k < 61; k++) { ll x = 1LL << k; if ((n & x) && x < y) { ll s = 1, t = 1; for (int i = 0; i < 61; i++) { ll z = 1LL << i; if (z >= y) break; if (z < x) { s *= 4; s %= P; t *= 3; t %= P; } else { s *= 2; s %= P; t *= ((n & z) && z > x) ? 1 : 2; t %= P; } } r += s - t; } } r %= P; if (r < 0) r += P; cout << r << endl; return 0; }