#include <bits/stdc++.h>
using namespace std;
long long n;
const int mod = 1e9 + 7;
int power(int x, int y) {
  int ret = 1;
  while (y > 0) {
    if ((y & 1) == 1) ret = 1ll * x * ret % mod;
    x = 1ll * x * x % mod, y >>= 1;
  }
  return ret;
}
const int inv2 = 500000004, inv6 = 166666668;
long long _n;
int calc(long long l) {
  l %= mod;
  return ((l * (l - 1) % mod * (l * 2 - 1) % mod -
           _n * (_n + 1) % mod * (_n * 2 + 1) % mod) *
              inv6 % mod +
          (_n + 1) * (_n + l) % mod * (_n - l + 1) % mod * inv2 % mod) %
         mod;
}
int fac[9], iFac[9], pre[9], suf[9], val[9];
int lagrange(int x) {
  if (x <= 8) return val[x];
  pre[1] = 1;
  for (int i = 1; i < 8; ++i) pre[i + 1] = 1ll * (x - i) * pre[i] % mod;
  suf[8] = 1;
  for (int i = 8; i > 1; --i) suf[i - 1] = 1ll * (x - i) * suf[i] % mod;
  int ret = 0;
  for (int i = 1; i <= 8; ++i)
    ret = (((8 - i) & 1 ? -1ll : 1ll) * iFac[i - 1] * iFac[8 - i] % mod *
               pre[i] % mod * suf[i] % mod * val[i] +
           ret) %
          mod;
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  _n = n % mod;
  fac[0] = 1;
  for (int i = 1; i <= 8; ++i) fac[i] = 1ll * i * fac[i - 1] % mod;
  iFac[8] = power(fac[8], mod - 2);
  for (int i = 8; i > 0; --i) iFac[i - 1] = 1ll * i * iFac[i] % mod;
  const double eps = 1e-9;
  int ans = 0;
  for (long long x = 0; x * x <= n; ++x) {
    long long k = sqrt(n - x * x) + eps;
    int temp = calc(x * x);
    for (int y = 1; y <= 8; ++y)
      val[y] = (calc(x * x + y * y) + val[y - 1]) % mod;
    temp = (2ll * lagrange(k % mod) + temp) % mod;
    if (x == 0)
      ans = (temp + ans) % mod;
    else
      ans = (2ll * temp + ans) % mod;
  }
  cout << (ans + mod) % mod << endl;
  return 0;
}
