#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
const long long mod = 1e9 + 7;
long long m, rev6, sum2[maxn], sum4[maxn], sum6[maxn];
long long exgcd(long long a, long long b, long long& x, long long& y) {
  if (a == 0 && b == 0) return -1;
  if (b == 0) {
    x = 1, y = 0;
    return a;
  }
  long long d = exgcd(b, a % b, y, x);
  y -= a / b * x;
  return d;
}
long long rev(long long a, long long n) {
  long long x, y;
  long long d = exgcd(a, n, x, y);
  return (x % n + n) % n;
}
long long qmod(long long a, long long n) {
  long long ret = 1;
  while (n) {
    if (n & 1) ret = ret * a % mod;
    a = a * a % mod;
    n = n >> 1;
  }
  return ret;
}
void init() {
  for (long long i = 1; i < maxn; ++i) {
    sum2[i] = (qmod(i, 2) + sum2[i - 1]) % mod;
    sum4[i] = (qmod(i, 4) + sum4[i - 1]) % mod;
    sum6[i] = (qmod(i, 6) + sum6[i - 1]) % mod;
  }
  rev6 = rev(6, mod);
}
int main() {
  init();
  while (scanf("%lld", &m) == 1) {
    long long bound = sqrt(double(m));
    long long ans = 0, keep = m;
    m %= mod;
    for (long long x = -bound; x <= bound; ++x) {
      long long y = sqrt((double)keep - (double)x * x);
      long long part1 = m * (m + 1) % mod * (m + 2) % mod;
      long long x6 = qmod(x, 6), x4 = qmod(x, 4), x2 = qmod(x, 2);
      long long y2 = (sum2[y] << 1) % mod, y4 = (sum4[y] << 1) % mod,
                y6 = (sum6[y] << 1) % mod;
      part1 = (part1 + x6 * 2) % mod;
      part1 = (part1 - x4 * (m + 2) % mod * 3 % mod + mod) % mod;
      part1 = (part1 + x2 * (3 * m + 4) % mod) % mod;
      part1 = part1 * (2 * y + 1) % mod;
      long long part2 = x4 * y2 % mod * 6 % mod;
      part2 = (part2 + x2 * y4 % mod * 6 % mod) % mod;
      part2 = (part2 + 2 * y6 % mod) % mod;
      part2 = (part2 -
               (x2 * y2 * 2 % mod + y4) % mod * (m + 2) % mod * 3 % mod + mod) %
              mod;
      part2 = (part2 + (3 * m + 4) * y2 % mod) % mod;
      ans = (ans + part1 + part2) % mod;
    }
    ans = ans * rev6 % mod;
    printf("%lld\n", ans);
  }
  return 0;
}
