Problem p03332 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define Endl endl
#define mp make_pair
#define rep(N) for (int i = 0; i < N; i++)
#define repj(N) for (int j = 0; j < N; j++)
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define For(I, N) for (int I = 0; I < N; I++)
#define cinone(N)                                                              \
  int N;                                                                       \
  cin >> N;
#define scanfone(N)                                                            \
  int N;                                                                       \
  cin >> N;
#define cinng(N, M)                                                            \
  int N[M];                                                                    \
  for (int yiuytvnm = 0; yiuytvnm < M; yiuytvnm++)                             \
    cin >> N[yiuytvnm];
#define scanfng(N, M)                                                          \
  int N[M];                                                                    \
  for (int qrwuoiq = 0; qrwuoiq < M; qrwuoiq++)                                \
    scanf("%d", &N[qrwuoiq]);
#define over(A)                                                                \
  {                                                                            \
    cout << A << endl;                                                         \
    exit(0);                                                                   \
  }
typedef unsigned long long ull;
const int inf = 103974182;
const int mod = 998244353;
using namespace std;
ll n, a, b;
ll k;
ll e[300005];
ll ny[300005];

ll C(int a, int b) { return (((e[a] * ny[a - b]) % mod) * ny[b]) % mod; }

int lenof(ll a) {
  int res = 0;
  while (a) {
    a /= 10;
    res++;
  }
  return res;
}

ll cal(int x) {
  //	cout<<x<<endl;
  ll basic = x;
  int tm = mod - 2;
  ll res = 1;
  while (tm) {
    if (tm & 1) {
      res = res * basic;
      //			cout<<res<<endl;
      //			cout<<lenof(res)<<endl;
      res = res % 998244353LL;
    }
    tm >>= 1;
    basic = basic * basic;
    basic = basic % 998244353LL;
  }
  return res;
}

int main() {
  //	freopen("input.txt","r",stdin);
  e[1] = 1;
  e[0] = 1;
  ny[1] = cal(1);
  ny[0] = 1;
  for (int i = 2; i < 300004; i++) {
    e[i] = 1LL * e[i - 1] * i;
    e[i] %= 998244353LL;
    ny[i] = cal(e[i]);
    //		cout<<ny[i]<<endl;
  }
  cin >> n >> a >> b >> k;
  if ((a + b) * n < k)
    over("0\n");
  ll res = 0;
  //	cout<<C(5,2)<<endl;
  for (int i = 0; i <= n; i++) {
    if ((k - 1LL * i * a) % b)
      continue;
    int x = (k - 1LL * i * a) / b;
    res += C(n, i) * C(n, x);
    res %= mod;
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define Endl endl
#define mp make_pair
#define rep(N) for (int i = 0; i < N; i++)
#define repj(N) for (int j = 0; j < N; j++)
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define For(I, N) for (int I = 0; I < N; I++)
#define cinone(N)                                                              \
  int N;                                                                       \
  cin >> N;
#define scanfone(N)                                                            \
  int N;                                                                       \
  cin >> N;
#define cinng(N, M)                                                            \
  int N[M];                                                                    \
  for (int yiuytvnm = 0; yiuytvnm < M; yiuytvnm++)                             \
    cin >> N[yiuytvnm];
#define scanfng(N, M)                                                          \
  int N[M];                                                                    \
  for (int qrwuoiq = 0; qrwuoiq < M; qrwuoiq++)                                \
    scanf("%d", &N[qrwuoiq]);
#define over(A)                                                                \
  {                                                                            \
    cout << A << endl;                                                         \
    exit(0);                                                                   \
  }
typedef unsigned long long ull;
const int inf = 103974182;
const int mod = 998244353;
using namespace std;
ll n, a, b;
ll k;
ll e[300005];
ll ny[300005];

ll C(int a, int b) { return (((e[a] * ny[a - b]) % mod) * ny[b]) % mod; }

int lenof(ll a) {
  int res = 0;
  while (a) {
    a /= 10;
    res++;
  }
  return res;
}

ll cal(int x) {
  //	cout<<x<<endl;
  ll basic = x;
  int tm = mod - 2;
  ll res = 1;
  while (tm) {
    if (tm & 1) {
      res = res * basic;
      //			cout<<res<<endl;
      //			cout<<lenof(res)<<endl;
      res = res % 998244353LL;
    }
    tm >>= 1;
    basic = basic * basic;
    basic = basic % 998244353LL;
  }
  return res;
}

int main() {
  //	freopen("input.txt","r",stdin);
  e[1] = 1;
  e[0] = 1;
  ny[1] = cal(1);
  ny[0] = 1;
  for (int i = 2; i < 300004; i++) {
    e[i] = 1LL * e[i - 1] * i;
    e[i] %= 998244353LL;
    ny[i] = cal(e[i]);
    //		cout<<ny[i]<<endl;
  }
  cin >> n >> a >> b >> k;
  if ((a + b) * n < k)
    over("0\n");
  ll res = 0;
  //	cout<<C(5,2)<<endl;
  for (int i = 0; i <= n; i++) {
    if ((k - 1LL * i * a) % b)
      continue;
    int x = (k - 1LL * i * a) / b;
    if (x < 0 || x > n)
      continue;
    res += C(n, i) * C(n, x);
    res %= mod;
  }
  cout << res << endl;
  return 0;
}