Problem p03022 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define clr(x) memset(x, 0, sizeof x)
#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Fod(i, b, a) for (int i = (b); i >= (a); i--)
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define outval(x) cerr << #x " = " << x << endl
#define outtag(x) cerr << "---------------" #x "---------------" << endl
#define outarr(a, L, R)                                                        \
  cerr << #a "[" << L << ".." << R << "] = ";                                  \
  For(_x, L, R) cerr << a[_x] << " ";                                          \
  cerr << endl;
using namespace std;
typedef long long LL;
LL read() {
  LL x = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch))
    f |= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return f ? -x : x;
}
const int N = 18, mod = 998244353;
int Pow(int x, int y) {
  int ans = 1;
  for (; y; y >>= 1, x = (LL)x * x % mod)
    if (y & 1)
      ans = (LL)ans * x % mod;
  return ans;
}
void Add(int &x, int y) {
  if ((x += y) >= mod)
    x -= mod;
}
void Del(int &x, int y) {
  if ((x -= y) < 0)
    x += mod;
}
int Add(int x) { return x >= mod ? x - mod : x; }
int Del(int x) { return x < 0 ? x + mod : x; }
int inv2 = (mod + 1) >> 1;
int n;
int a[N];
void FWT(int *a, int n, int flag) {
  for (int d = 1; d < n; d <<= 1)
    for (int i = 0; i < n; i += d << 1)
      for (int j = 0; j < d; j++) {
        int x = a[i + j], y = a[i + j + d];
        a[i + j] = Add(x + y);
        a[i + j + d] = Del(x - y);
        if (flag < 0) {
          a[i + j] = (LL)a[i + j] * inv2 % mod;
          a[i + j + d] = (LL)a[i + j + d] * inv2 % mod;
        }
      }
}
int main() {
  n = read(), n = 1 << n;
  int s = 0;
  For(i, 0, n - 1) a[i] = read(), s += a[i];
  s = Pow(s, mod - 2);
  For(i, 0, n - 1) a[i] = (LL)a[i] * s % mod;
  FWT(a, n, 1);
  Fod(i, n - 1, 0) {
    if (a[i] == 1) {
      a[i] = 0;
      continue;
    }
    a[i] = Pow((a[i] - 1 + mod) % mod, mod - 2);
  }
  FWT(a, n, 1);
  For(i, 0, n - 1) printf("%d\n", Del(a[i] - a[0]));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define clr(x) memset(x, 0, sizeof x)
#define For(i, a, b) for (int i = (a); i <= (b); i++)
#define Fod(i, b, a) for (int i = (b); i >= (a); i--)
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define outval(x) cerr << #x " = " << x << endl
#define outtag(x) cerr << "---------------" #x "---------------" << endl
#define outarr(a, L, R)                                                        \
  cerr << #a "[" << L << ".." << R << "] = ";                                  \
  For(_x, L, R) cerr << a[_x] << " ";                                          \
  cerr << endl;
using namespace std;
typedef long long LL;
LL read() {
  LL x = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch))
    f |= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return f ? -x : x;
}
const int N = 18, mod = 998244353;
int Pow(int x, int y) {
  int ans = 1;
  for (; y; y >>= 1, x = (LL)x * x % mod)
    if (y & 1)
      ans = (LL)ans * x % mod;
  return ans;
}
void Add(int &x, int y) {
  if ((x += y) >= mod)
    x -= mod;
}
void Del(int &x, int y) {
  if ((x -= y) < 0)
    x += mod;
}
int Add(int x) { return x >= mod ? x - mod : x; }
int Del(int x) { return x < 0 ? x + mod : x; }
int inv2 = (mod + 1) >> 1;
int n;
int a[1 << N];
void FWT(int *a, int n, int flag) {
  for (int d = 1; d < n; d <<= 1)
    for (int i = 0; i < n; i += d << 1)
      for (int j = 0; j < d; j++) {
        int x = a[i + j], y = a[i + j + d];
        a[i + j] = Add(x + y);
        a[i + j + d] = Del(x - y);
        if (flag < 0) {
          a[i + j] = (LL)a[i + j] * inv2 % mod;
          a[i + j + d] = (LL)a[i + j + d] * inv2 % mod;
        }
      }
}
int main() {
  n = read(), n = 1 << n;
  int s = 0;
  For(i, 0, n - 1) a[i] = read(), s += a[i];
  s = Pow(s, mod - 2);
  For(i, 0, n - 1) a[i] = (LL)a[i] * s % mod;
  FWT(a, n, 1);
  Fod(i, n - 1, 0) {
    if (a[i] == 1) {
      a[i] = 0;
      continue;
    }
    a[i] = Pow((a[i] - 1 + mod) % mod, mod - 2);
  }
  FWT(a, n, 1);
  For(i, 0, n - 1) printf("%d\n", Del(a[i] - a[0]));
  return 0;
}