Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define ll long long
#define int long long
using namespace std;
const int maxn = 500010, inf = 1e9, mod = 998244353;
int n, a, b, K, ans;
int fac[maxn], inv[maxn];

template <typename T> inline void read(T &k) {
  int f = 1;
  k = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c == '-' && (f = -1), c = getchar();
  while (c <= '9' && c >= '0')
    k = k * 10 + c - '0', c = getchar();
  k *= f;
}

ll exgcd(ll a, ll b, ll &x, ll &y) {
  if (!b)
    return x = 1, y = 0, a;
  int ans = exgcd(b, a % b, x, y);
  ll t = x;
  x = y;
  y = t - a / b * y;
  return ans;
}

inline int power(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1, a = 1ll * a * a % mod)
    if (b & 1)
      ans = 1ll * ans * a % mod;
  return ans;
}

inline int C(int n, int m) {
  return (n < m) ? 0 : 1ll * fac[n] * inv[m] % mod * inv[n - m] % mod;
}

signed main() {
  //	freopen("1.out", "w", stdout);
  read(n);
  read(a);
  read(b);
  read(K);
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = 1ll * fac[i - 1] * i % mod;
  inv[n] = power(fac[n], mod - 2);
  for (int i = n; i; i--)
    inv[i - 1] = 1ll * inv[i] * i % mod;
  int x, y;
  int d = exgcd(a, b, x, y);
  //	printf("%lld %lld %lld\n", d, x, y);
  int tmp = K;
  if (tmp % d)
    return puts("0"), 0;
  int tmpx = x * tmp / d, tmpy = y * tmp / d;
  //		printf("tmp:%lld %lld %lld\n", tmpx, tmpy, tmp);
  int TMP = tmpx / (b / d);
  tmpx -= TMP * (b / d), tmpy += TMP * (a / d);
  //		printf("l:%lld tmpx:%lld tmpy:%lld\n", l, tmpx, tmpy);
  for (; tmpx <= n && tmpy >= 0;)
    //		printf("tmpx:%lld tmpy:%lld %lld\n", tmpx, tmpy, ans),
    ans = (ans + C(n, tmpx) * C(n, tmpy)) % mod, tmpx += b / d, tmpy -= a / d;
  printf("%lld\n", ans);
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define ll long long
#define int long long
using namespace std;
const int maxn = 500010, inf = 1e9, mod = 998244353;
int n, a, b, K, ans;
int fac[maxn], inv[maxn];

template <typename T> inline void read(T &k) {
  int f = 1;
  k = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c == '-' && (f = -1), c = getchar();
  while (c <= '9' && c >= '0')
    k = k * 10 + c - '0', c = getchar();
  k *= f;
}

ll exgcd(ll a, ll b, ll &x, ll &y) {
  if (!b)
    return x = 1, y = 0, a;
  int ans = exgcd(b, a % b, x, y);
  ll t = x;
  x = y;
  y = t - a / b * y;
  return ans;
}

inline int power(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1, a = 1ll * a * a % mod)
    if (b & 1)
      ans = 1ll * ans * a % mod;
  return ans;
}

inline int C(int n, int m) {
  return (n < m) ? 0 : 1ll * fac[n] * inv[m] % mod * inv[n - m] % mod;
}

signed main() {
  //	freopen("1.out", "w", stdout);
  read(n);
  read(a);
  read(b);
  read(K);
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = 1ll * fac[i - 1] * i % mod;
  inv[n] = power(fac[n], mod - 2);
  for (int i = n; i; i--)
    inv[i - 1] = 1ll * inv[i] * i % mod;
  int x, y;
  int d = exgcd(a, b, x, y);
  //	printf("%lld %lld %lld\n", d, x, y);
  int tmp = K;
  if (tmp % d)
    return puts("0"), 0;
  int tmpx = x * tmp / d, tmpy = y * tmp / d;
  //		printf("tmp:%lld %lld %lld\n", tmpx, tmpy, tmp);
  int TMP = tmpx / (b / d);
  tmpx -= TMP * (b / d), tmpy += TMP * (a / d);
  //		printf("l:%lld tmpx:%lld tmpy:%lld\n", l, tmpx, tmpy);
  for (; tmpx <= n && tmpy >= 0;)
  //		printf("tmpx:%lld tmpy:%lld %lld\n", tmpx, tmpy, ans),
  {
    if (tmpx >= 0 && tmpy <= n)
      ans = (ans + C(n, tmpx) * C(n, tmpy)) % mod;
    tmpx += b / d, tmpy -= a / d;
  }
  printf("%lld\n", ans);
}