#include <bits/stdc++.h>
using namespace std;
long long M, K, ans;
long long aa;
long long bb[7];
long long ii[10];
long long c[10][10];
long long cc[4];
long long inv(long long x) {
  long long g[30];
  long long cur = 1;
  g[0] = x;
  for (int i = 1; i < 30; i++) {
    g[i] = g[i - 1] * g[i - 1] % 1000000007LL;
  }
  for (int i = 0; i < 30; i++) {
    if ((1 << i) & (1000000007LL - 2)) {
      cur = cur * g[i] % 1000000007LL;
    }
  }
  return cur;
}
long long sum(long long a, long long b) {
  long long cur = 0;
  long long cur2 = 1;
  for (int i = b; i >= 0; i--) {
    cur2 = cur2 * a % 1000000007LL;
    cur += ii[b + 1] * c[b + 1][i] % 1000000007LL * bb[i] % 1000000007LL * cur2;
    cur %= 1000000007LL;
  }
  return cur;
}
int main() {
  bb[0] = 1;
  bb[1] = inv(2);
  bb[2] = inv(6);
  bb[4] = inv(1000000007LL - 30);
  bb[6] = inv(42);
  for (int i = 0; i < 10; i++) {
    c[i][0] = 1;
    for (int j = 1; j <= i; j++) {
      c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
    }
    if (i >= 1) ii[i] = inv(i);
  }
  cin >> M;
  cc[1] = (M + 2) % 1000000007LL;
  cc[2] = M % 1000000007LL * ((M - 1) % 1000000007LL) % 1000000007LL * ii[2] %
          1000000007LL;
  cc[3] = M % 1000000007LL * ((M + 1) % 1000000007LL) % 1000000007LL *
          ((M + 2) % 1000000007LL) % 1000000007LL * ii[6] % 1000000007LL;
  for (long long i = 1; i <= sqrt(M); i++) {
    K = i * i % 1000000007LL;
    aa = sqrt(M - i * i);
    ans += (cc[3] +
            (1000000007LL - cc[1]) * K % 1000000007LL * (1000000007LL + K - 1) %
                1000000007LL * ii[2] % 1000000007LL +
            (K + 1) % 1000000007LL * K % 1000000007LL * (K + 1000000007LL - 1) %
                1000000007LL * ii[3]) %
           1000000007LL;
    ans += (2 * ii[3] % 1000000007LL *
            ((K * K % 1000000007LL * K % 1000000007LL * aa % 1000000007LL +
              3 * K * K % 1000000007LL * sum(aa, 2) % 1000000007LL +
              3 * K * sum(aa, 4) % 1000000007LL + sum(aa, 6) % 1000000007LL +
              ((1000000007LL - K) % 1000000007LL) % 1000000007LL * aa +
              (1000000007LL - sum(aa, 2)) % 1000000007LL) %
             1000000007LL)) %
           1000000007LL;
    ans += (2 * aa * cc[3] % 1000000007LL);
    ans += (1000000007LL - cc[1]) *
           (aa * K % 1000000007LL * K % 1000000007LL +
            (2 * sum(aa, 2) - aa + 1000000007LL) * K % 1000000007LL +
            sum(aa, 4) + (1000000007LL - sum(aa, 2))) %
           1000000007LL;
    ans %= 1000000007LL;
  }
  ans *= 2;
  int i = 0;
  K = i * i % 1000000007LL;
  aa = sqrt(M - i * i);
  ans += (cc[3] +
          (1000000007LL - cc[1]) * K % 1000000007LL * (1000000007LL + K - 1) %
              1000000007LL * ii[2] % 1000000007LL +
          (K + 1) % 1000000007LL * K % 1000000007LL * (K + 1000000007LL - 1) %
              1000000007LL * ii[3]) %
         1000000007LL;
  ans += (2 * ii[3] % 1000000007LL *
          ((K * K % 1000000007LL * K % 1000000007LL * aa % 1000000007LL +
            3 * K * K % 1000000007LL * sum(aa, 2) % 1000000007LL +
            3 * K * sum(aa, 4) % 1000000007LL + sum(aa, 6) % 1000000007LL +
            ((1000000007LL - K) % 1000000007LL) % 1000000007LL * aa +
            (1000000007LL - sum(aa, 2)) % 1000000007LL) %
           1000000007LL)) %
         1000000007LL;
  ans += (2 * aa * cc[3] % 1000000007LL);
  ans += (1000000007LL - cc[1]) *
         (aa * K % 1000000007LL * K % 1000000007LL +
          (2 * sum(aa, 2) - aa + 1000000007LL) * K % 1000000007LL + sum(aa, 4) +
          (1000000007LL - sum(aa, 2))) %
         1000000007LL;
  ans %= 1000000007LL;
  cout << ans << endl;
}
