#include <bits/stdc++.h>
using namespace std;
long long inv30, inv6, inv3, inv2, inv42;
long long qpow(long long a, long long b) {
  long long ret = 1;
  for (; b; b >>= 1, a = a * a % 1000000007)
    if (b & 1) ret = ret * a % 1000000007;
  return ret;
}
long long cal1(long long b) {
  return b %= 1000000007, b * (b + 1) / 2 % 1000000007;
}
long long cal2(long long b) {
  return b %= 1000000007, b * (b + 1) % 1000000007 * (2 * b + 1) % 1000000007 *
                              inv6 % 1000000007;
}
long long cal3(long long b) { return cal1(b) * cal1(b) % 1000000007; }
long long cal4(long long b) {
  return b %= 1000000007, b * (b + 1) % 1000000007 * (2 * b + 1) % 1000000007 *
                              (3 * b * b % 1000000007 + 3 * b - 1) %
                              1000000007 * inv30 % 1000000007;
}
long long cal6(long long b) {
  return b %= 1000000007,
         b * (b + 1) % 1000000007 * (2 * b + 1) % 1000000007 *
             (3 * b * (b * b % 1000000007 * (b + 2) % 1000000007 - 1) %
                  1000000007 +
              1) %
             1000000007 * inv42 % 1000000007;
}
int main() {
  long long n, ans = 0, ans1 = 0;
  inv30 = qpow(30, 1000000007 - 2);
  inv42 = qpow(42, 1000000007 - 2);
  inv3 = qpow(3, 1000000007 - 2);
  inv6 = qpow(6, 1000000007 - 2);
  inv2 = qpow(2, 1000000007 - 2);
  cin >> n;
  for (long long a = 1, b = 1e6; a * a <= n; ++a) {
    while (a * a + b * b > n) b--;
    long long A = a * a % 1000000007;
    ans = (ans + cal2(n) - cal2(A - 1) + 1000000007 + cal1(n) - cal1(A - 1) +
           1000000007 +
           (n - A + 1) % 1000000007 * (-A * A % 1000000007 + A + 1000000007) %
               1000000007) %
          1000000007;
    if (!b) continue;
    ans = (ans + (cal2(n) + cal1(n)) * b) % 1000000007;
    ans = (ans + (inv3 + n + 1) % 1000000007 * (A * b % 1000000007 + cal2(b))) %
          1000000007;
    ans = (ans + (-2 - n) % 1000000007 *
                     (A * A % 1000000007 * b % 1000000007 +
                      2ll * A * cal2(b) % 1000000007 + cal4(b))) %
          1000000007;
    ans = (ans +
           (-inv3 + 1) * (A * A % 1000000007 * A % 1000000007 * b % 1000000007 +
                          3ll * A * A % 1000000007 * cal2(b) % 1000000007 +
                          3ll * A * cal4(b) % 1000000007 + cal6(b))) %
          1000000007;
  }
  ans = 2ll * ans % 1000000007;
  ans = (ans + (cal2(n) + cal1(n)) * (1000000007 + 1) / 2) % 1000000007;
  printf("%lld", (ans % 1000000007 + 1000000007) % 1000000007);
}
