#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long ans;
long long sum2[1000005], sum4[1000005], sum6[1000005];
long long calc(long long t3, long long t2, long long t1, long long t0,
               long long l, long long r) {
  long long ans = 1ll * t0 * r % 1000000007;
  ans += 1ll * t1 * l % 1000000007 * r % 1000000007 +
         1ll * t1 * sum2[r] % 1000000007;
  ans += 1ll * t2 * l % 1000000007 * l % 1000000007 * r % 1000000007 +
         2ll * t2 * l % 1000000007 * sum2[r] % 1000000007 +
         1ll * t2 * sum4[r] % 1000000007;
  ans += 1ll * t3 * l % 1000000007 * l % 1000000007 * l % 1000000007 * r %
             1000000007 +
         3ll * t3 * l % 1000000007 * l % 1000000007 * sum2[r] % 1000000007 +
         3ll * t3 * l % 1000000007 * sum4[r] % 1000000007 +
         1ll * t3 * sum6[r] % 1000000007;
  return ans % 1000000007;
}
signed main() {
  scanf("%lld", &m);
  long long tmp = 1;
  for (; 1ll * tmp * tmp <= m; tmp++)
    ;
  for (long long i = 1; i < tmp; i++) {
    sum2[i] = 1ll * i * i % 1000000007;
    sum4[i] = 1ll * sum2[i] * i % 1000000007 * i % 1000000007;
    sum6[i] = 1ll * sum4[i] * i % 1000000007 * i % 1000000007;
  }
  for (long long i = 1; i < tmp; i++) {
    sum2[i] = (sum2[i] + sum2[i - 1]) % 1000000007;
    sum4[i] = (sum4[i] + sum4[i - 1]) % 1000000007;
    sum6[i] = (sum6[i] + sum6[i - 1]) % 1000000007;
  }
  long long r = tmp, M = m % 1000000007;
  long long cs = (1ll * M * M % 1000000007 * M % 1000000007 +
                  3ll * M * M % 1000000007 + 2ll * M) %
                 1000000007;
  for (long long i = 1; 1ll * i * i <= m; i++) {
    for (; 1ll * r * r + 1ll * i * i > m; r--)
      ;
    ans += calc(2, ((-3 * m - 6) % 1000000007 + 1000000007) % 1000000007,
                (3 * m + 4) % 1000000007, cs, 1ll * i * i % 1000000007, r);
  }
  for (long long i = 1; i < tmp; i++) {
    long long p = (m - 1ll * i * i + 1) % 1000000007;
    ans += 1ll * p * (p + 1) % 1000000007 *
           (m % 1000000007 + 2ll * i * i % 1000000007) % 1000000007;
  }
  ans = ans % 1000000007 * 4 % 1000000007 * 166666668 % 1000000007;
  m %= 1000000007;
  ans += 1ll * m * (m + 1) / 2 % 1000000007 * (m + 1) % 1000000007 -
         1ll * m * (m + 1) % 1000000007 * (2 * m + 1) % 1000000007 * 166666668 %
             1000000007;
  printf("%lld\n", (ans + 233ll * 1000000007) % 1000000007);
}
