#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  register long long data = 0, w = 1;
  char ch = 0;
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  if (ch == '-') w = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    data = (data << 1) + (data << 3) + (ch ^ 48), ch = getchar();
  return data * w;
}
long long n, f[1000005], N, inv[1000005];
const long long mod = 1e9 + 7;
long long KSM(long long x, long long k) {
  long long tmp = 1, y = x;
  for (long long i = k; i; i >>= 1, y = 1ll * y * y % mod)
    if (i & 1) tmp = 1ll * tmp * y % mod;
  return tmp;
}
void init(long long n = 8) {
  inv[0] = inv[1] = 1;
  for (long long i = 2; i <= n; i++)
    inv[i] = 1ll * inv[mod % i] * (mod - mod / i) % mod;
}
const long long i2 = KSM(2, mod - 2), i6 = KSM(6, mod - 2);
long long S(long long a) {
  return 1ll * i6 * a % mod * (a + 1) % mod * (2 * a + 1) % mod;
}
long long F(long long a) {
  return a %= mod, (i2 * (1 + N) % mod * (N + a) % mod * (N - a + 1) % mod -
                    S(N) + S(a - 1)) %
                       mod;
}
long long Lagr(long long x) {
  long long ans = 0;
  for (long long i = 0; i <= 7; i++) {
    long long s1 = 1, s2 = 1;
    for (long long j = 0; j <= i - 1; j++) {
      s1 = s1 * (x - j) % mod;
      s2 = s2 * inv[i - j] % mod;
    }
    for (long long j = i + 1; j <= 7; j++) {
      s1 = s1 * (x - j) % mod;
      s2 = -s2 * inv[j - i] % mod;
    }
    ans = (ans + s1 * s2 % mod * f[i]) % mod;
  }
  return ans;
}
int main() {
  init();
  n = read();
  N = n % mod;
  long long L = sqrt(n), ans = 0;
  for (long long i = 0; i <= L; i++) {
    long long r = sqrt(n - i * i);
    f[0] = F(i * i);
    for (long long j = 1; j <= 7; j++)
      f[j] = (f[j - 1] + 2ll * F(i * i + j * j)) % mod;
    ans = (ans + (1ll + (i > 0)) * Lagr(r)) % mod;
  }
  cout << (ans + mod) % mod << '\n';
  return 0;
}
