#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
long long int powmod(long long int a, long long int e) {
  long long int sum = 1;
  long long int cur = a;
  while (e > 0) {
    if (e % 2) {
      sum = sum * cur % mod;
    }
    cur = cur * cur % mod;
    e /= 2;
  }
  return sum;
}
void add(long long int &x, long long int y) { x = (x + y) % mod; }
void mul(long long int &x, long long int y) { x = (x * y) % mod; }
long long int bern[7] = {
    1, (mod + 1) / 2,      (mod + 1) / 6, 0, powmod(mod - 30, mod - 2),
    0, powmod(42, mod - 2)};
long long int inv[8];
void init() {
  for (int i = (int)(1); i < (int)(8); i++) inv[i] = powmod(i, mod - 2);
}
long long int comb[8][8] = {
    {1},
    {1, 1},
    {1, 2, 1},
    {1, 3, 3, 1},
    {1, 4, 6, 4, 1},
    {1, 5, 10, 10, 5, 1},
    {1, 6, 15, 21, 15, 6, 1},
    {1, 7, 21, 35, 35, 21, 7, 1},
};
unordered_map<long long int, long long int> memo[7];
long long int accum(long long int x, int d) {
  if (x == 0) {
    return d == 0 ? 1 : 0;
  }
  if (d == 0) {
    return (2 * x + 1) % mod;
  }
  long long int tot = 0;
  for (int i = (int)(0); i < (int)(d + 1); i++) {
    long long int tmp = bern[i];
    for (int _ = (int)(0); _ < (int)(d + 1 - i); _++) {
      mul(tmp, x % mod);
    }
    mul(tmp, comb[d + 1][i]);
    add(tot, tmp);
  }
  mul(tot, inv[d + 1]);
  mul(tot, 2);
  return tot;
}
long long int coef_final(long long int m, int d) {
  long long int lim = sqrt(m) + 2;
  long long int tot = 0;
  long long int ylim = lim;
  for (long long int x = 0; x <= lim; ++x) {
    if (x * x > m) continue;
    while (ylim * ylim > m - x * x) {
      ylim--;
    }
    long long int xx = x * x % mod;
    for (int i = (int)(0); i < (int)(d + 1); i++) {
      long long int tmp = accum(ylim, 2 * i);
      for (int j = (int)(0); j < (int)(d - i); j++) mul(tmp, xx);
      mul(tmp, comb[d][i]);
      add(tot, tmp * (x == 0 ? 1 : 2));
    }
  }
  return tot;
}
long long int smart2(long long int m) {
  long long int tot = 0;
  long long int prod = m % mod;
  mul(prod, (m + 1) % mod);
  mul(prod, (m + 2) % mod);
  add(tot, coef_final(m, 0) * prod);
  add(tot, coef_final(m, 1) * ((3 * m + 4) % mod));
  prod = (3 * m + 6) % mod;
  mul(prod, (mod - 1) % mod);
  add(tot, coef_final(m, 2) * prod);
  add(tot, coef_final(m, 3) * 2);
  return tot * ((mod + 1) / 6) % mod;
}
int main(void) {
  init();
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long int m;
  cin >> m;
  cout << smart2(m) << "\n";
}
