Problem p02768 (C++)

Buggy submission (status: Time Limit Exceeded):
// Created by sz
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int mod = 1e9 + 7;
struct modular {
  int value;
  modular(long long v = 0) {
    value = v % mod;
    if (value < 0)
      value += mod;
  }
  modular(long long a, long long b) : value(0) {
    *this += a;
    *this /= b;
  }
  modular &operator+=(modular const &b) {
    value += b.value;
    if (value >= mod)
      value -= mod;
    return *this;
  }
  modular &operator-=(modular const &b) {
    value -= b.value;
    if (value < 0)
      value += mod;
    return *this;
  }
  modular &operator*=(modular const &b) {
    value = (long long)value * b.value % mod;
    return *this;
  }
  // 其他函数调用mexp(modular(10),3);
  friend modular mexp(modular a, long long e) {
    modular res = 1;
    while (e) {
      if (e & 1)
        res *= a;
      a *= a;
      e >>= 1;
    }
    return res;
  }
  friend modular inverse(modular a) { return mexp(a, mod - 2); }
  modular &operator/=(modular const &b) { return *this *= inverse(b); }
  friend modular operator+(modular a, modular const b) { return a += b; }
  friend modular operator-(modular a, modular const b) { return a -= b; }
  friend modular operator-(modular const a) { return 0 - a; }
  friend modular operator*(modular a, modular const b) { return a *= b; }
  // 除法只有在mod是素数可用;
  friend modular operator/(modular a, modular const b) { return a /= b; }
  friend ostream &operator<<(ostream &os, modular const &a) {
    return os << a.value;
  }
  friend istream &operator>>(istream &is, modular &a) {
    long long value;
    is >> value;
    a.value = 0;
    a += value;
    return is;
  }
  friend bool operator==(modular const &a, modular const &b) {
    return a.value == b.value;
  }
  friend bool operator!=(modular const &a, modular const &b) {
    return a.value != b.value;
  }
};

ll quick_pow(ll a, ll b) {
  ll res = 1;
  a %= mod;
  while (b) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}

int main() {
#ifdef LOCAL
  freopen("./input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n, a, b;
  cin >> n >> a >> b;

  modular ans = 1;

  for (int i = 0; i < n; i++) {
    ans *= 2;
  }

  ans -= 1;

  vector<int> fac(max(a, b) + 1), finv(max(a, b) + 1);
  int to = max(a, b);
  fac[0] = finv[0] = 1;
  for (int i = 1; i <= to; ++i)
    fac[i] = (ll)fac[i - 1] * i % mod;

  finv[to] = quick_pow(fac[to], mod - 2);
  for (int i = to - 1; i; --i)
    finv[i] = (ll)finv[i + 1] * (i + 1) % mod;

  auto combo = [&](int n, int k) {
    modular res = 1;
    for (int i = 0; i < k; i++) {
      res *= n - i;
    }

    res *= finv[k];
    return res;
  };

  ans -= combo(n, a);
  ans -= combo(n, b);
  cout << ans << "\n";

  return 0;
}


Fixed submission:
// Created by sz
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int mod = 1e9 + 7;
struct modular {
  int value;
  modular(long long v = 0) {
    value = v % mod;
    if (value < 0)
      value += mod;
  }
  modular(long long a, long long b) : value(0) {
    *this += a;
    *this /= b;
  }
  modular &operator+=(modular const &b) {
    value += b.value;
    if (value >= mod)
      value -= mod;
    return *this;
  }
  modular &operator-=(modular const &b) {
    value -= b.value;
    if (value < 0)
      value += mod;
    return *this;
  }
  modular &operator*=(modular const &b) {
    value = (long long)value * b.value % mod;
    return *this;
  }
  // 其他函数调用mexp(modular(10),3);
  friend modular mexp(modular a, long long e) {
    modular res = 1;
    while (e) {
      if (e & 1)
        res *= a;
      a *= a;
      e >>= 1;
    }
    return res;
  }
  friend modular inverse(modular a) { return mexp(a, mod - 2); }
  modular &operator/=(modular const &b) { return *this *= inverse(b); }
  friend modular operator+(modular a, modular const b) { return a += b; }
  friend modular operator-(modular a, modular const b) { return a -= b; }
  friend modular operator-(modular const a) { return 0 - a; }
  friend modular operator*(modular a, modular const b) { return a *= b; }
  // 除法只有在mod是素数可用;
  friend modular operator/(modular a, modular const b) { return a /= b; }
  friend ostream &operator<<(ostream &os, modular const &a) {
    return os << a.value;
  }
  friend istream &operator>>(istream &is, modular &a) {
    long long value;
    is >> value;
    a.value = 0;
    a += value;
    return is;
  }
  friend bool operator==(modular const &a, modular const &b) {
    return a.value == b.value;
  }
  friend bool operator!=(modular const &a, modular const &b) {
    return a.value != b.value;
  }
};

ll quick_pow(ll a, ll b) {
  ll res = 1;
  a %= mod;
  while (b) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}

int main() {
#ifdef LOCAL
  freopen("./input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n, a, b;
  cin >> n >> a >> b;

  modular ans = quick_pow(2, n);

  ans -= 1;

  vector<int> fac(max(a, b) + 1), finv(max(a, b) + 1);
  int to = max(a, b);
  fac[0] = finv[0] = 1;
  for (int i = 1; i <= to; ++i)
    fac[i] = (ll)fac[i - 1] * i % mod;

  finv[to] = quick_pow(fac[to], mod - 2);
  for (int i = to - 1; i; --i)
    finv[i] = (ll)finv[i + 1] * (i + 1) % mod;

  auto combo = [&](int n, int k) {
    modular res = 1;
    for (int i = 0; i < k; i++) {
      res *= n - i;
    }

    res *= finv[k];
    return res;
  };

  ans -= combo(n, a);
  ans -= combo(n, b);
  cout << ans << "\n";

  return 0;
}