Problem p01939 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

LL n, m;
LL f(LL a) { return (m - a) / (n - 1) + a / (n - 1) + 1; }
LL powm(LL a, LL b) {
  if (b == 0)
    return 1;
  return powm(a * a % MOD, b / 2) * (b % 2 ? a : 1) % MOD;
}
LL inv(LL x) { return powm(x, MOD - 2); }

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  cin >> n >> m;

  if (n == 1) {
    cout << (m + 1) % MOD << endl;
    return 0;
  }

  LL d = m / (n - 1) % MOD, r = (m - d % MOD * (n - 1) % MOD) % MOD;
  m %= MOD;
  n %= MOD;
  cout << (m + 1 +
           2 * (d * (d - 1) % MOD * inv(2) % MOD * (n - 1) % MOD +
                d * (r + 1) % MOD)) %
              MOD
       << endl;

  return 0;
}

Error: 1

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

LL n, m;
LL f(LL a) { return (m - a) / (n - 1) + a / (n - 1) + 1; }
LL powm(LL a, LL b) {
  if (b == 0)
    return 1;
  return powm(a * a % MOD, b / 2) * (b % 2 ? a : 1) % MOD;
}
LL inv(LL x) { return powm(x, MOD - 2); }

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  cin >> n >> m;

  if (n == 1) {
    cout << m + 1 << endl;
    return 0;
  }

  LL d = m / (n - 1) % MOD, r = (m - d * (n - 1)) % MOD;
  m %= MOD;
  n %= MOD;
  cout << (m + 1 +
           2 * (d * (d - 1) % MOD * inv(2) % MOD * (n - 1) % MOD +
                d * (r + 1) % MOD)) %
              MOD
       << endl;
  return 1;

  return 0;
}