Language: C++

#include <bits/stdc++.h>
using namespace std;
const int BASE = 1e9 + 7;

long long fact[600600], invFact[600600];

long long power(long long x, long long y)
{
  if (!y)
    return 1;
  long long res = power(x, y >> 1);
  res = res * res % BASE;
  if (y & 1)
    res = res * x % BASE;
  return res;
}

long long c(int n, int k)
{
  return n < k ? 0 : fact[n] * invFact[k] % BASE * invFact[n - k] % BASE;
}

// m numbers >= x, rest <= y
long long calc(int n, int m, int r, int x, int y)
{
  long long res = 0;
  // i of n - m > y
  for (int i = 0; i <= n - m; i++)
  {
    int rem = r - x * m - i * (y + 1);
    if (rem < 0)
      break;
    long long ways = c(n + rem, n) * c(n - m, i) % BASE;
    if (i % 2) res = (res - ways + BASE) % BASE;
    else res = (res + ways) % BASE;
  }
  return res;
}

long long solve(int n, int m, int r)
{
  long long res = c(n + r, n), bad = 0;
  for (int x = 1; x * m <= r; x++)
    bad = (bad + calc(n, m, r, x, x - 1) - calc(n, m, r, x + 1, x - 1) + BASE) % BASE;
  bad = bad * c(n, m) % BASE;
  return (res - bad + BASE) % BASE;
}

int main()
{
  for (int i = 0; i <= 600000; i++)
  {
    fact[i] = i ? fact[i - 1] * i % BASE : 1;
    invFact[i] = power(fact[i], BASE - 2);
  }

  int m, n, l, r;
  cin >> n >> m >> l >> r;
  cout << (solve(n, m, r) - solve(n, m, l - 1) + BASE) % BASE << endl;
}
#include <bits/stdc++.h>
using namespace std;
const int BASE = 1e9 + 7;

long long fact[600600], invFact[600600];

long long power(long long x, long long y)
{
  if (!y)
    return 1;
  long long res = power(x, y >> 1);
  res = res * res % BASE;
  if (y & 1)
    res = res * x % BASE;
  return res;
}

long long c(int n, int k)
{
  return n < k ? 0 : fact[n] * invFact[k] % BASE * invFact[n - k] % BASE;
}

// m numbers >= x, rest <= y
long long calc(int n, int m, int r, int x, int y)
{
  long long res = 0;
  // i of n - m > y
  for (int i = 0; i <= n - m; i++)
  {
    int rem = r - x * m - i * (y + 1);
    if (rem < 0)
      break;
    long long ways = c(n + rem, n) * c(n - m, i) % BASE;
    if (i % 2) res = (res - ways + BASE) % BASE;
    else res = (res + ways) % BASE;
  }
  return res;
}

long long solve(int n, int m, int r)
{
  long long res = c(n + r, n), bad = 0;
  for (int x = 1; x * m <= r; x++)
    bad = (bad + calc(n, m, r, x, x - 1) - calc(n, m, r, x + 1, x - 1) + BASE) % BASE;
  bad = bad * c(n, m) % BASE;
  return (res - bad + BASE) % BASE;
}

int main()
{
  for (int i = 0; i <= 600000; i++)
  {
    fact[i] = i ? fact[i - 1] * i % BASE : 1;
    invFact[i] = power(fact[i], BASE - 2);
  }

  int m, n, l, r;
  cin >> n >> m >> l >> r;
  cout << (solve(n, m, r) - solve(n, m, l - 1) + BASE) % BASE << endl;
}


Compiler output:
a.cc:63:11: error: redefinition of 'const int BASE'
   63 | const int BASE = 1e9 + 7;
      |           ^~~~
a.cc:3:11: note: 'const int BASE' previously defined here
    3 | const int BASE = 1e9 + 7;
      |           ^~~~
a.cc:65:11: error: redefinition of 'long long int fact [600600]'
   65 | long long fact[600600], invFact[600600];
      |           ^~~~
a.cc:5:11: note: 'long long int fact [600600]' previously declared here
    5 | long long fact[600600], invFact[600600];
      |           ^~~~
a.cc:65:25: error: redefinition of 'long long int invFact [600600]'
   65 | long long fact[600600], invFact[600600];
      |                         ^~~~~~~
a.cc:5:25: note: 'long long int invFact [600600]' previously declared here
    5 | long long fact[600600], invFact[600600];
      |                         ^~~~~~~
a.cc:67:11: error: redefinition of 'long long int power(long long int, long long int)'
   67 | long long power(long long x, long long y)
      |           ^~~~~
a.cc:7:11: note: 'long long int power(long long int, long long int)' previously defined here
    7 | long long power(long long x, long long y)
      |           ^~~~~
a.cc:78:11: error: redefinition of 'long long int c(int, int)'
   78 | long long c(int n, int k)
      |           ^
a.cc:18:11: note: 'long long int c(int, int)' previously defined here
   18 | long long c(int n, int k)
      |           ^
a.cc:84:11: error: redefinition of 'long long int calc(int, int, int, int, int)'
   84 | long long calc(int n, int m, int r, int x, int y)
      |           ^~~~
a.cc:24:11: note: 'long long int calc(int, int, int, int, int)' previously defined here
   24 | long long calc(int n, int m, int r, int x, int y)
      |           ^~~~
a.cc:100:11: error: redefinition of 'long long int solve(int, int, int)'
  100 | long long solve(int n, int m, int r)
      |           ^~~~~
a.cc:40:11: note: 'long long int solve(int, int, int)' previously defined here
   40 | long long solve(int n, int m, int r)
      |           ^~~~~
a.cc:109:5: error: redefinition of 'int main()'
  109 | int main()
      |     ^~~~
a.cc:49:5: note: 'int main()' previously defined here
   49 | int main()
      |     ^~~~