#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7, N = 1e6 + 100, inv2 = (mod + 1) >> 1,
          inv6 = (mod + 1) / 6;
long long n, nn, Ans, s1[N], s2[N], s3[N];
void Inc(long long &x, long long y) { x += y, x = x >= mod ? x - mod : x; }
int main() {
  scanf("%lld", &n);
  nn = n % mod;
  for (long long x = 1, tp = 1; x * x <= n; ++x, tp = x * x % mod) {
    s1[x] = s1[x - 1], s2[x] = s2[x - 1], s3[x] = s3[x - 1], Inc(s1[x], tp),
    Inc(s2[x], tp * tp % mod), Inc(s3[x], tp * tp % mod * tp % mod);
  }
  for (long long x = 0; x * x <= n; ++x) {
    long long y = sqrt(n - x * x), tp = x * x % mod;
    long long tmp = 0;
    Inc(tmp, (nn + 1) * inv2 % mod * (nn * nn % mod + nn + tp - tp * tp % mod) %
                 mod * (2 * y + 1) % mod);
    Inc(tmp, mod - (nn + 1) * inv2 % mod * (2 * tp + mod - 1) % mod * s1[y] %
                       mod * 2 % mod);
    Inc(tmp, mod - (nn + 1) % mod * inv2 % mod * s2[y] % mod * 2 % mod);
    Inc(tmp, inv6 * (6 * tp % mod * tp % mod - 6 * tp % mod + mod + 1) % mod *
                 s1[y] % mod * 2 % mod);
    Inc(tmp, inv6 * (6 * tp % mod + mod - 3) % mod * s2[y] % mod * 2 % mod);
    Inc(tmp, inv6 * (4 * s3[y]) % mod);
    Inc(tmp, inv6 *
                 (2 * tp % mod * tp % mod * tp % mod - 3 * tp * tp % mod + mod +
                  tp) %
                 mod * (2 * y + 1) % mod);
    Inc(tmp, mod - (2 * nn + 1) * inv6 % mod * nn % mod * (nn + 1) % mod *
                       (2 * y + 1) % mod);
    if (x) Inc(tmp, tmp);
    Inc(Ans, tmp);
  }
  printf("%lld", Ans);
  return 0;
}
