#include <bits/stdc++.h>
const int mod = 1000000007;
const int inv2 = (mod + 1) / 2;
const int inv3 = (mod + 1) / 3;
const int inv6 = (mod + 1) / 6;
const int inv7 = (mod + 1) / 7;
const int inv30 = 233333335;
inline long long sum1(long long n) {
  n %= mod;
  return n * (n + 1) % mod * inv2 % mod;
}
inline long long sum2(long long n) {
  n %= mod;
  return n * (n + 1) % mod * (2 * n + 1) % mod * inv6 % mod;
}
inline long long sum3(long long n) {
  n = sum1(n);
  return n * n % mod;
}
inline long long sum4(long long n) {
  n %= mod;
  return n * (n + 1) % mod * (2 * n + 1) % mod *
         (3 * (n + 1) % mod * n % mod - 1) % mod * inv30 % mod;
}
inline long long sum5(long long n) {
  n %= mod;
  return sum3(n) * (2 * (n + 1) % mod * n % mod - 1) * inv3 % mod;
}
inline long long sum6(long long n) {
  n %= mod;
  return sum2(n) * (((3 * n + 6) % mod * n % mod * n % mod - 3) * n % mod + 1) %
         mod * inv7 % mod;
}
inline long long sqrt(long long n) { return (long long)sqrt((double)n); }
inline long long t1(long long x, long long y) {
  x %= mod;
  y %= mod;
  return (x * (y + 1) % mod + sum2(y)) % mod;
}
inline long long t2(long long x, long long y) {
  x %= mod;
  y %= mod;
  return (x * x % mod * (y + 1) % mod + 2 * x % mod * sum2(y) % mod + sum4(y)) %
         mod;
}
inline long long t3(long long x, long long y) {
  x %= mod;
  y %= mod;
  return (x * x % mod * x % mod * (y + 1) % mod +
          3 * x % mod * x % mod * sum2(y) % mod + 3 * x % mod * sum4(y) % mod +
          sum6(y) % mod) %
         mod;
}
int main() {
  long long m;
  scanf("%I64d", &m);
  long long ans = 0, y = 0;
  while (y * y <= m) ++y;
  for (long long x = 1; x * x <= m; ++x) {
    while (x * x + y * y > m) --y;
    ans = (ans + (y + 1) % mod * (sum2(m) + sum1(m))) % mod;
    ans = (ans + (1 - inv3) * t3(x * x, y)) % mod;
    ans = (ans - (m + 2) % mod * t2(x * x, y)) % mod;
    ans = (ans + (m + 1 - inv6 + inv2) % mod * t1(x * x, y)) % mod;
  }
  ans = ans * 2 % mod;
  ans = (ans + inv2 * (sum1(m) + sum2(m))) % mod;
  printf("%d\n", (int)((ans + mod) % mod));
  return 0;
}
