#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long p2(long long x) { return x * x % mod; }
long long p4(long long x) { return p2(x) * p2(x) % mod; }
long long p6(long long x) { return p2(x) * p4(x) % mod; }
long long s2[1000010], s4[1000010], s6[1000010];
long long qp(long long a, long long p) {
  long long ans = 1;
  for (; p; p >>= 1, a = a * a % mod)
    if (p & 1) ans = ans * a % mod;
  return ans;
}
int main() {
  int i;
  long long M, x;
  for (i = 1; i < 1000010; i++) {
    s2[i] = (s2[i - 1] + p2(i)) % mod;
    s4[i] = (s4[i - 1] + p4(i)) % mod;
    s6[i] = (s6[i - 1] + p6(i)) % mod;
  }
  scanf("%lld", &M);
  long long mx = (long long)sqrt(M), inv6 = qp(6, mod - 2), m = M % mod,
            ans = 0;
  for (x = 0; x <= mx; x++) {
    long long y = (long long)sqrt(M - x * x), temp = 0, x2 = p2(x), x4 = p4(x),
              x6 = p6(x);
    temp += m * (m + 1) % mod * (m + 2) % mod * (2 * y + 1) % mod;
    temp -= ((x4 - x2) * (2 * y + 1) % mod + (2 * x2 - 1) * 2 * s2[y] % mod +
             2 * s4[y]) %
            mod * (3 * m + 4) % mod;
    temp +=
        2 *
        ((x6 - x4) * (2 * y + 1) % mod + (3 * x4 - 2 * x2) * 2 * s2[y] % mod +
         (3 * x2 - 1) * 2 * s4[y] % mod + 2 * s6[y]) %
        mod;
    temp = temp * inv6 % mod;
    if (!x)
      (ans += temp) %= mod;
    else
      (ans += 2 * temp) %= mod;
  }
  printf("%lld", (ans + mod) % mod);
  return 0;
}
