#include <bits/stdc++.h>
using namespace std;
long long MOD = 1e9 + 7;
long long N;
long long ans;
long long exp(long long a, long long b) {
  if (b == 0) return 1;
  long long c = exp(a, b / 2);
  c = (c * c) % MOD;
  if (b % 2) return (c * a) % MOD;
  return c;
}
long long inv(long long x) { return exp(x, MOD - 2); }
long long A[50];
int main() {
  cin >> N;
  long long T = N + 1;
  long long init = T % 2;
  long long rds = (T - init) / 2 + 1;
  ans = (((T + init) % MOD * rds) % MOD * inv(2)) % MOD;
  ans = (ans + (N % MOD)) % MOD;
  long long cur = 1;
  long long ind = 0;
  long long minus = 0;
  long long i = 1;
  for (; ind * 2 + 1 <= N; ++i) {
    ind = ind * 2 + 1;
    minus = (minus + cur) % MOD;
    A[i - 1] = cur;
    cur = (cur * 3) % MOD;
  }
  N -= ind;
  long long mult = 1;
  for (; i > -1; --i) {
    if (N < (1LL << i)) continue;
    N -= (1LL << i);
    minus += mult * A[i];
    mult = (mult * 2) % MOD;
  }
  ans = ((ans + MOD) - minus) % MOD;
  cout << ans;
}
