#include <bits/stdc++.h>
using namespace std;
long long int N, M, m = 1000000007, a[64], b, k = 1, l, c, i, o = 1;
int main() {
  scanf("%lld", &N);
  M = ((++N) ^ (N / 2));
  for (i = 64; i; ++c) {
    if (M & (o << --i)) {
      swap(k, l);
      l += k * c;
      l %= m;
      c = 0;
    }
    a[i] = (N & (o << i)) ? l : (-l);
  }
  for (k = 0, l = 1; i < 64 - __builtin_ctzll(M); ++i) {
    k += a[i] * l;
    l *= 3;
    k %= m;
    l %= m;
  }
  cout << k << endl;
  return 0;
}
