#include <bits/stdc++.h>
using namespace std;
const int P = 1000000007;
long long m, finv[11], py[11], inv[11], pre[11], suf[11];
long long qp(long long x, long long y) {
  long long z = 1;
  for (x %= P; y; y >>= 1, x = x * x % P) {
    if (y & 1) z = z * x % P;
  }
  return z;
}
int main() {
  scanf("%lld", &m);
  for (long long i = finv[0] = 1; i <= 10; i++) {
    inv[i] = qp(i, P - 2);
    finv[i] = qp(i, P - 2) * finv[i - 1] % P;
  }
  auto calc = [&](long long n, long long tar) {
    pre[0] = suf[n + 1] = 1;
    long long ans = 0;
    for (long long i = n; i; i--) {
      suf[i] = suf[i + 1] * (tar - i + P) % P;
    }
    for (long long i = 1; i <= n; i++) {
      pre[i] = pre[i - 1] * (tar - i + P) % P;
      long long prod = pre[i - 1] * suf[i + 1] % P * finv[i - 1] % P *
                       finv[n - i] % P * ((n - i) & 1 ? P - 1 : 1) % P;
      (ans += prod * py[i]) %= P;
    }
    return ans;
  };
  auto S2 = [&](long long n) {
    if (n < 0) return 0LL;
    long long x = n, y = n + 1, z = 2 * n + 1;
    if (!(x % 2))
      x /= 2;
    else
      y /= 2;
    if (!(x % 3))
      x /= 3;
    else if (!(y % 3))
      y /= 3;
    else
      z /= 3;
    return (x % P) * (y % P) % P * (z % P) % P;
  };
  auto get = [&](long long l, long long r) {
    long long x = l + r, y = r - l + 1;
    if (!(x % 2))
      x /= 2;
    else
      y /= 2;
    return (x % P) * (y % P) % P;
  };
  long long ans = 0, tmp = round(sqrt(m)) + 10;
  for (long long x = 1; x <= tmp; x++) {
    if (x * x > m) continue;
    long long y = m - x * x, z = round(sqrt(y)) + 10;
    while (z * z > y) z--;
    for (long long i = 1, cur = 0; i <= min(z, 8LL); i++) {
      long long r = i * i + x * x;
      cur += S2(r - 1) - S2(m) + (m + 1) % P * get(r, m) % P;
      cur = (cur % P + P) % P;
      py[i] = cur;
    }
    (ans += calc(min(z, 8LL), z)) %= P;
  }
  (ans *= 4) %= P;
  for (long long x = -tmp; x <= tmp; x++) {
    if (x * x > m) continue;
    long long r = x * x;
    long long coef = (S2(r - 1) - S2(m) + (m + 1) % P * get(r, m) % P + P) % P;
    (ans += coef * (x != 0 ? 2 : 1)) %= P;
  }
  printf("%lld\n", ans);
  return 0;
}
