Problem p02875 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
ll modinv(ll a, ll m) {
  assert(m > 0);
  if (m == 1)
    return 0;
  a %= m;
  if (a < 0)
    a += m;
  assert(a != 0);
  if (a == 1)
    return 1;
  return m - modinv(m, a) * m / a;
}

template <int MOD_> struct modnum {
private:
  int v;

public:
  static const int MOD = MOD_;

  modnum() : v(0) {}
  modnum(ll v_) : v(int(v_ % MOD)) {
    if (v < 0)
      v += MOD;
  }
  explicit operator int() const { return v; }
  friend bool operator==(const modnum &a, const modnum &b) {
    return a.v == b.v;
  }
  friend bool operator!=(const modnum &a, const modnum &b) {
    return a.v != b.v;
  }

  modnum operator~() const {
    modnum res;
    res.v = modinv(v, MOD);
    return res;
  }

  modnum &operator+=(const modnum &o) {
    v += o.v;
    if (v >= MOD)
      v -= MOD;
    return *this;
  }
  modnum &operator-=(const modnum &o) {
    v -= o.v;
    if (v < 0)
      v += MOD;
    return *this;
  }
  modnum &operator*=(const modnum &o) {
    v = int(ll(v) * ll(o.v) % MOD);
    return *this;
  }
  modnum &operator/=(const modnum &o) { return *this *= (~o); }

  friend modnum operator+(const modnum &a, const modnum &b) {
    return modnum(a) += b;
  }
  friend modnum operator-(const modnum &a, const modnum &b) {
    return modnum(a) -= b;
  }
  friend modnum operator*(const modnum &a, const modnum &b) {
    return modnum(a) *= b;
  }
  friend modnum operator/(const modnum &a, const modnum &b) {
    return modnum(a) /= b;
  }
};

using num = modnum<998244353>;

vector<num> fact;
vector<num> ifact;

void init() {
  fact = {1};
  for (int i = 1; i < 11000000; i++)
    fact.push_back(i * fact[i - 1]);
  for (num x : fact)
    ifact.push_back(1 / x);
}

num ncr(int n, int k) {
  if (k < 0 || k > n)
    return 0;
  return fact[n] * ifact[k] * ifact[n - k];
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  init();
  num ans = 1;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    ans *= 3;
  vector<num> p2(n + 1, 0);
  p2[0] = 1;
  for (int i = 1; i <= n; i++) {
    p2[i] = 2 * p2[i - 1];
  }
  for (int i = 0; i < n / 2; i++) {
    ans -= 2 * p2[i] * ncr(n, i);
  }
  cout << (int)ans << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
ll modinv(ll a, ll m) {
  assert(m > 0);
  if (m == 1)
    return 0;
  a %= m;
  if (a < 0)
    a += m;
  assert(a != 0);
  if (a == 1)
    return 1;
  return m - modinv(m, a) * m / a;
}

template <int MOD_> struct modnum {
private:
  int v;

public:
  static const int MOD = MOD_;

  modnum() : v(0) {}
  modnum(ll v_) : v(int(v_ % MOD)) {
    if (v < 0)
      v += MOD;
  }
  explicit operator int() const { return v; }
  friend bool operator==(const modnum &a, const modnum &b) {
    return a.v == b.v;
  }
  friend bool operator!=(const modnum &a, const modnum &b) {
    return a.v != b.v;
  }

  modnum operator~() const {
    modnum res;
    res.v = modinv(v, MOD);
    return res;
  }

  modnum &operator+=(const modnum &o) {
    v += o.v;
    if (v >= MOD)
      v -= MOD;
    return *this;
  }
  modnum &operator-=(const modnum &o) {
    v -= o.v;
    if (v < 0)
      v += MOD;
    return *this;
  }
  modnum &operator*=(const modnum &o) {
    v = int(ll(v) * ll(o.v) % MOD);
    return *this;
  }
  modnum &operator/=(const modnum &o) { return *this *= (~o); }

  friend modnum operator+(const modnum &a, const modnum &b) {
    return modnum(a) += b;
  }
  friend modnum operator-(const modnum &a, const modnum &b) {
    return modnum(a) -= b;
  }
  friend modnum operator*(const modnum &a, const modnum &b) {
    return modnum(a) *= b;
  }
  friend modnum operator/(const modnum &a, const modnum &b) {
    return modnum(a) /= b;
  }
};

using num = modnum<998244353>;

vector<num> fact;
vector<num> ifact;

void init() {
  fact = {1};
  for (int i = 1; i < 11000000; i++)
    fact.push_back(i * fact[i - 1]);
  ifact.resize(fact.size());
  ifact.back() = 1 / fact.back();
  for (int i = (int)fact.size() - 1; i >= 1; i--) {
    ifact[i - 1] = i * ifact[i];
  }
}

num ncr(int n, int k) {
  if (k < 0 || k > n)
    return 0;
  return fact[n] * ifact[k] * ifact[n - k];
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  init();
  num ans = 1;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    ans *= 3;
  vector<num> p2(n + 1, 0);
  p2[0] = 1;
  for (int i = 1; i <= n; i++) {
    p2[i] = 2 * p2[i - 1];
  }
  for (int i = 0; i < n / 2; i++) {
    ans -= 2 * p2[i] * ncr(n, i);
  }
  cout << (int)ans << '\n';
}