#include <bits/stdc++.h>
using namespace std;
long long fact[200005];
long long extgcd(long long a, long long b, long long& x, long long& y) {
  long long d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}
long long mod_inverse(long long a, long long m) {
  long long x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}
long long euler_phi(long long n) {
  long long res = n;
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      res = res / i * (i - 1);
      for (; n % i == 0; n /= i)
        ;
    }
  }
  if (n != 1) res = res / n * (n - 1);
  return res;
}
long long euler[100000];
void euler_phi2() {
  for (long long i = 0; i < 100000; i++) euler[i] = i;
  for (long long i = 2; i < 100000; i++) {
    if (euler[i] == i) {
      for (int j = i; j < 100000; j += i) euler[j] = euler[j] / i * (i - 1);
    }
  }
}
long long mod_pow(long long x, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1) res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}
long long mod_fact(long long n, long long p, long long& e) {
  e = 0;
  if (n == 0) return 1;
  long long res = mod_fact(n / p, p, e);
  e += n / p;
  if (n / p % 2 != 0) return res * (p - fact[n % p]) % p;
  return res * fact[n % p] % p;
}
long long mod_comb(long long n, long long k, long long p) {
  if (n == k || k == 0) return 1;
  long long e1, e2, e3;
  long long a1 = mod_fact(n, p, e1), a2 = mod_fact(k, p, e2),
            a3 = mod_fact(n - k, p, e3);
  if (e1 > e2 + e3) return 0;
  return a1 * mod_inverse(a2 * a3 % p, p) % p;
}
long long calc(long long n, long long i) {
  long long res = 0;
  return res;
}
int main() {
  long long n;
  cin >> n;
  long long i = 0;
  while ((1LL << (i + 1)) - 2 <= n) i++;
  long long ans = (mod_pow(3, i, 1000000007) + 1000000007 - 1) % 1000000007;
  ans = (ans * mod_inverse(2, 1000000007)) % 1000000007;
  long long j = (1L << i) - 2, k = n - j;
  if (k) {
    if (k == 1)
      ans = (ans + 1) % 1000000007;
    else if (k == (1LL << i)) {
      ans = (ans +
             (((mod_pow(3, i, 1000000007) - 3 + 1000000007) % 1000000007) *
              mod_inverse(2, 1000000007)) %
                 1000000007 +
             1) %
            1000000007;
    } else if (k == (1LL << i) + 1) {
      ans = (ans +
             (((mod_pow(3, i, 1000000007) - 3 + 1000000007) % 1000000007) *
              mod_inverse(2, 1000000007)) %
                 1000000007 +
             3) %
            1000000007;
    } else if (k < (1LL << i)) {
      ans = (ans + calc(n, k - 1) + 1) % 1000000007;
    } else {
      ans = (ans + mod_pow(3, i, 1000000007) - calc(n, (1LL << i) - k + 1)) %
            1000000007;
    }
  }
  cout << ans % 1000000007 << endl;
  return 0;
}
