prithivMLmods/Coder-Stat huggingface dataset:
Problem p04003 (Python)

Buggy submission (status: Runtime Error):
import sys
from collections import deque, defaultdict


def bfs01(s, t, links):
    q = deque([(0, s, -1)])  # cost, station, last-company
    visited = set()
    while q:
        d, v, e = q.popleft()
        if v == t:
            return d
        if (v, e) in visited:
            continue
        visited.add((v, e))
        if e == 0:
            for c, us in links[v]:
                for u in us:
                    if (u, c) in visited:
                        continue
                    q.append((d + 1, u, c))
        else:
            for u in links[v][e]:
                if (u, e) in visited:
                    continue
                q.appendleft((d, u, e))
            if (v, 0) not in visited:
                q.appendleft((d, v, 0))

    return -1


readline = sys.stdin.buffer.readline
read = sys.stdin.buffer.read

n, m = map(int, readline().split())
links = [defaultdict(set) for _ in range(n)]
pqc = list(map(int, read().split()))
for p, q, c in zip(pqc[0::3], pqc[1::3], pqc[2::3]):
    p -= 1
    q -= 1
    links[p][c].add(q)
    links[q][c].add(p)

print(bfs01(0, n - 1, links))


Fixed submission:
import sys
from collections import deque, defaultdict


def bfs01(s, t, links):
    q = deque([(0, s, -1)])  # cost, station, last-company
    visited = set()
    while q:
        d, v, e = q.popleft()
        if v == t:
            return d
        if (v, e) in visited:
            continue
        visited.add((v, e))
        if e == 0:
            for c, us in links[v].items():
                for u in us:
                    if (u, c) in visited:
                        continue
                    q.append((d + 1, u, c))
        else:
            for u in links[v][e]:
                if (u, e) in visited:
                    continue
                q.appendleft((d, u, e))
            if (v, 0) not in visited:
                q.appendleft((d, v, 0))

    return -1


readline = sys.stdin.buffer.readline
read = sys.stdin.buffer.read

n, m = map(int, readline().split())
links = [defaultdict(set) for _ in range(n)]
pqc = list(map(int, read().split()))
for p, q, c in zip(pqc[0::3], pqc[1::3], pqc[2::3]):
    p -= 1
    q -= 1
    links[p][c].add(q)
    links[q][c].add(p)

print(bfs01(0, n - 1, links))
Problem p04003 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define len(x) (int)(x).size()
#define ins insert
typedef long long ll;
typedef long double ld;
using namespace std;
const int N = 1e5, inf = 1e9;
map<int, set<int>> g[N];
bool used[N];
int d[N];
deque<int> layer;
set<pair<int, int>> was;
void dfs(int &st, int v, int C) {
  d[v] = min(d[v], d[st] + 1);
  layer.pb(v);
  was.insert({v, C});
  for (auto u : g[v][C])
    if (was.find({u, C}) == was.end())
      dfs(st, u, C);
}
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int p, q, c;
    cin >> p >> q >> c;
    --p, --q;
    g[p][c].ins(q);
    g[q][c].ins(p);
  }

  fill(d, d + N, inf);
  d[0] = 0;
  layer.pb(0);
  while (!layer.empty()) {
    int head = layer.front();
    layer.pop_front();
    for (auto C : g[head]) {
      for (auto U : C.se)
        if (was.find({U, C.fi}) == was.end())
          dfs(head, U, C.fi);
    }
  }
  if (d[n - 1] == inf)
    cout << -1;
  else
    cout << d[n - 1] << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define len(x) (int)(x).size()
#define ins insert
typedef long long ll;
typedef long double ld;
using namespace std;
const int N = 1e5, inf = 1e9;
map<int, set<int>> g[N];
bool used[N];
int d[N];
deque<int> layer;
set<pair<int, int>> was;
void dfs(int &st, int v, int C) {
  if (d[v] > d[st] + 1) {
    d[v] = d[st] + 1;
    layer.pb(v);
  }
  was.insert({v, C});
  for (auto u : g[v][C])
    if (was.find({u, C}) == was.end())
      dfs(st, u, C);
}
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int p, q, c;
    cin >> p >> q >> c;
    --p, --q;
    g[p][c].ins(q);
    g[q][c].ins(p);
  }

  fill(d, d + N, inf);
  d[0] = 0;
  layer.pb(0);
  while (!layer.empty()) {
    int head = layer.front();
    layer.pop_front();
    for (auto C : g[head]) {
      for (auto U : C.se)
        if (was.find({U, C.fi}) == was.end())
          dfs(head, U, C.fi);
    }
  }
  if (d[n - 1] == inf)
    cout << -1;
  else
    cout << d[n - 1] << '\n';
  return 0;
}
Problem p04003 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, c, cnt;
int d[500020];
vector<pair<int, int>> a[500020];
map<int, int> g[100020];
deque<int> q;
int get(int x, int y) {
  if (g[x].count(y)) {
    return g[x][y];
  }
  return g[x][y] = ++cnt;
}
void add(int x, int y, int z) {
  a[x].push_back(make_pair(y, z));
  a[y].push_back(make_pair(x, z));
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    get(i, 0);
  }
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &x, &y, &c);
    int x0 = get(x, 0), xc = get(x, c);
    int y0 = get(y, 0), yc = get(y, c);
    add(xc, yc, 0);
    add(x0, yc, 1);
    add(xc, y0, 1);
  }
  memset(d, -1, sizeof d);
  d[1] = 0;
  q.push_back(1);
  while (q.size()) {
    int u = q.back();
    q.pop_back();
    for (pair<int, int> i : a[u]) {
      if (d[i.first] == -1 || d[i.first] > d[u] + i.second) {
        d[i.first] = d[u] + i.second;
        if (i.second == 0) {
          q.push_front(i.first);
        } else {
          q.push_back(i.first);
        }
      }
    }
  }
  printf("%d\n", d[n] >> 1);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, c, cnt;
int d[500020];
vector<pair<int, int>> a[500020];
map<int, int> g[100020];
deque<int> q;
int get(int x, int y) {
  if (g[x].count(y)) {
    return g[x][y];
  }
  return g[x][y] = ++cnt;
}
void add(int x, int y, int z) {
  a[x].push_back(make_pair(y, z));
  a[y].push_back(make_pair(x, z));
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    get(i, 0);
  }
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &x, &y, &c);
    int x0 = get(x, 0), xc = get(x, c);
    int y0 = get(y, 0), yc = get(y, c);
    add(xc, yc, 0);
    add(x0, yc, 1);
    add(xc, y0, 1);
  }
  memset(d, -1, sizeof d);
  d[1] = 0;
  q.push_back(1);
  while (q.size()) {
    int u = q.back();
    q.pop_back();
    for (pair<int, int> i : a[u]) {
      if (d[i.first] == -1 || d[i.first] > d[u] + i.second) {
        d[i.first] = d[u] + i.second;
        if (i.second == 1) {
          q.push_front(i.first);
        } else {
          q.push_back(i.first);
        }
      }
    }
  }
  printf("%d\n", d[n] >> 1);
  return 0;
}
Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5, M = 2e5 + 5;
const long long inf = 1e16;
map<pair<int, int>, int> id;
int lastid;
vector<pair<int, int>> adj[N + M];
long long dist[N + M];

void addEdge(int u, int v, int c1, int c2) {
  adj[u].push_back({v, c1});
  adj[v].push_back({u, c2});
}

int getId(int u, int c) {
  if (id.count({u, c}))
    return id[{u, c}];
  addEdge(u, lastid, 1, 0);
  id[{u, c}] = lastid;
  return lastid++;
}

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  lastid = n + 1;
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    a = getId(a, c);
    b = getId(b, c);
    addEdge(a, b, 0, 0);
  }
  for (int i = 0; i < lastid; i++)
    dist[i] = inf;
  set<pair<long long, int>> st;
  st.insert({0, 1});
  dist[1] = 0;
  while (!st.empty()) {
    auto it = st.begin();
    int v = it->second;
    long long now = it->first;
    st.erase(it);
    if (v == n) {
      printf("%lld\n", now);
      return 0;
    }
    for (auto it : adj[v]) {
      long long cur = now + it.second;
      if (dist[it.first] > cur) {
        st.erase(make_pair(dist[it.first], it.first));
        dist[it.first] = cur;
        st.insert(make_pair(cur, it.first));
      }
    }
  }
  puts("-1");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5, M = 2e6 + 5;
const long long inf = 1e16;
map<pair<int, int>, int> id;
int lastid;
vector<pair<int, int>> adj[N + M];
long long dist[N + M];

void addEdge(int u, int v, int c1, int c2) {
  adj[u].push_back({v, c1});
  adj[v].push_back({u, c2});
}

int getId(int u, int c) {
  if (id.count({u, c}))
    return id[{u, c}];
  addEdge(u, lastid, 1, 0);
  id[{u, c}] = lastid;
  return lastid++;
}

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  lastid = n + 1;
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    a = getId(a, c);
    b = getId(b, c);
    addEdge(a, b, 0, 0);
  }
  for (int i = 0; i < lastid; i++)
    dist[i] = inf;
  set<pair<long long, int>> st;
  st.insert({0, 1});
  dist[1] = 0;
  while (!st.empty()) {
    auto it = st.begin();
    int v = it->second;
    long long now = it->first;
    st.erase(it);
    if (v == n) {
      printf("%lld\n", now);
      return 0;
    }
    for (auto it : adj[v]) {
      long long cur = now + it.second;
      if (dist[it.first] > cur) {
        st.erase(make_pair(dist[it.first], it.first));
        dist[it.first] = cur;
        st.insert(make_pair(cur, it.first));
      }
    }
  }
  puts("-1");
  return 0;
}
Problem p04003 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N (200009)
using namespace std;

struct Node {
  int num, dis;
  bool operator<(const Node &a) const { return dis > a.dis; }
};
struct Edge {
  int to, next, len;
} edge[N << 1];
int n, m, u, v, c, cnt, dis[N], vis[N];
int head[N], num_edge;
priority_queue<Node> q;
map<pair<int, int>, int> Map;

void add(int u, int v, int l) {
  edge[++num_edge].to = v;
  edge[num_edge].next = head[u];
  edge[num_edge].len = l;
  head[u] = num_edge;
}

void Dijkstra(int s) {
  memset(dis, 0x7f, sizeof(dis));
  dis[s] = 0;
  q.push((Node){s, 0});
  while (!q.empty()) {
    int x = q.top().num;
    q.pop();
    if (vis[x])
      continue;
    vis[x] = 1;
    for (int i = head[x]; i; i = edge[i].next)
      if (!vis[edge[i].to] && dis[x] + edge[i].len < dis[edge[i].to]) {
        dis[edge[i].to] = dis[x] + edge[i].len;
        q.push((Node){edge[i].to, dis[edge[i].to]});
      }
  }
}

int getid(int x, int y) {
  if (Map.find(make_pair(x, y)) != Map.end())
    return Map[make_pair(x, y)];
  return Map[make_pair(x, y)] = ++cnt;
}

int main() {
  cin >> n >> m;
  cnt = n;
  for (int i = 1; i <= m; ++i) {
    cin >> u >> v >> c;
    int id1 = getid(u, c), id2 = getid(v, c);
    add(id1, id2, 0);
    add(id2, id1, 0);
    add(u, id1, 1);
    add(id1, u, 1);
    add(v, id2, 1);
    add(id2, v, 1);
  }
  Dijkstra(1);
  cout << (dis[n] > 2e9 ? -1 : dis[n] / 2) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define N (600009)
using namespace std;

struct Node {
  int num, dis;
  bool operator<(const Node &a) const { return dis > a.dis; }
};
struct Edge {
  int to, next, len;
} edge[N << 1];
int n, m, u, v, c, cnt, dis[N], vis[N];
int head[N], num_edge;
priority_queue<Node> q;
map<pair<int, int>, int> Map;

void add(int u, int v, int l) {
  edge[++num_edge].to = v;
  edge[num_edge].next = head[u];
  edge[num_edge].len = l;
  head[u] = num_edge;
}

void Dijkstra(int s) {
  memset(dis, 0x7f, sizeof(dis));
  dis[s] = 0;
  q.push((Node){s, 0});
  while (!q.empty()) {
    int x = q.top().num;
    q.pop();
    if (vis[x])
      continue;
    vis[x] = 1;
    for (int i = head[x]; i; i = edge[i].next)
      if (!vis[edge[i].to] && dis[x] + edge[i].len < dis[edge[i].to]) {
        dis[edge[i].to] = dis[x] + edge[i].len;
        q.push((Node){edge[i].to, dis[edge[i].to]});
      }
  }
}

int getid(int x, int y) {
  if (Map.find(make_pair(x, y)) != Map.end())
    return Map[make_pair(x, y)];
  return Map[make_pair(x, y)] = ++cnt;
}

int main() {
  cin >> n >> m;
  cnt = n;
  for (int i = 1; i <= m; ++i) {
    cin >> u >> v >> c;
    int id1 = getid(u, c), id2 = getid(v, c);
    add(id1, id2, 0);
    add(id2, id1, 0);
    add(u, id1, 1);
    add(id1, u, 1);
    add(v, id2, 1);
    add(id2, v, 1);
  }
  Dijkstra(1);
  cout << (dis[n] > 2e9 ? -1 : dis[n] / 2) << endl;
}
Problem p04004 (C++)

Buggy submission (status: Runtime Error):
#include "cstdio"
#include "iostream"

using namespace std;

const int maxm = 3e5 + 10;
const int Mod = 1e9 + 7;

int N, M, K, Max;
long long Fac[maxm], Inv1[maxm], Inv2[maxm], P[maxm];

long long C(int x, int y) { return Fac[x] * Inv2[y] % Mod * Inv2[x - y] % Mod; }

int main() {
  register int i, j;
  register long long Ans = 0;
  scanf("%d%d%d", &N, &M, &K);
  Max = N + M + K;
  Fac[0] = Fac[1] = Inv1[0] = Inv1[1] = Inv2[0] = Inv2[1] = 1;
  P[0] = 1, P[1] = 3;
  for (i = 2; i <= Max; i++) {
    Fac[i] = Fac[i - 1] * i % Mod;
    Inv1[i] = (Mod - Mod / i) * Inv1[Mod % i] % Mod;
    Inv2[i] = Inv2[i - 1] * Inv1[i] % Mod;
    P[i] = P[i - 1] * 3 % Mod;
  }
  if (M < K)
    swap(M, K);
  for (i = 0, j = 1; i <= M + K; i++) {
    Ans = (Ans + C(N - 1 + i, N - 1) * P[M + K - i] % Mod * j) % Mod;
    if (i < K)
      j = j * 2 % Mod;
    else if (i >= M)
      j = ((j << 1) - C(i, K) - C(i, i - M) + (Mod << 1)) % Mod;
    else
      j = ((j << 1) - C(i, K) + Mod) % Mod;
  }
  printf("%lld", Ans);
  return 0;
}

Fixed submission:
#include "cstdio"
#include "iostream"

using namespace std;

const int maxm = 9e5 + 10;
const int Mod = 1e9 + 7;

int N, M, K, Max;
long long Fac[maxm], Inv1[maxm], Inv2[maxm], P[maxm];

long long C(int x, int y) { return Fac[x] * Inv2[y] % Mod * Inv2[x - y] % Mod; }

int main() {
  register int i, j;
  register long long Ans = 0;
  scanf("%d%d%d", &N, &M, &K);
  Max = N + M + K;
  Fac[0] = Fac[1] = Inv1[0] = Inv1[1] = Inv2[0] = Inv2[1] = 1;
  P[0] = 1, P[1] = 3;
  for (i = 2; i <= Max; i++) {
    Fac[i] = Fac[i - 1] * i % Mod;
    Inv1[i] = (Mod - Mod / i) * Inv1[Mod % i] % Mod;
    Inv2[i] = Inv2[i - 1] * Inv1[i] % Mod;
    P[i] = P[i - 1] * 3 % Mod;
  }
  if (M < K)
    swap(M, K);
  for (i = 0, j = 1; i <= M + K; i++) {
    Ans = (Ans + C(N - 1 + i, N - 1) * P[M + K - i] % Mod * j) % Mod;
    if (i < K)
      j = j * 2 % Mod;
    else if (i >= M)
      j = ((j << 1) - C(i, K) - C(i, i - M) + (Mod << 1)) % Mod;
    else
      j = ((j << 1) - C(i, K) + Mod) % Mod;
  }
  printf("%lld", Ans);
  return 0;
}
Problem p04004 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define SF scanf
#define PF printf
#define MAXN 900010
#define MOD 1000000007
using namespace std;
typedef long long ll;
ll inv[MAXN], fac[MAXN], pow3[MAXN];
ll fsp(ll x, int y) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    y >>= 1;
  }
  return res;
}
ll C(int x, int y) { return fac[x] * inv[y] % MOD * inv[x - y] % MOD; }
int main() {
  pow3[0] = 1;
  fac[0] = 1;
  for (int i = 1; i <= 900000; i++) {
    pow3[i] = pow3[i - 1] * 3ll % MOD;
    fac[i] = fac[i - 1] * i % MOD;
  }
  inv[900000] = fsp(fac[900000], MOD - 2);
  for (int i = 900000; i > 0; i--)
    inv[i - 1] = inv[i] * i % MOD;
  int n, m, k;
  while (SF("%d%d%d", &n, &m, &k)) {
    n--;
    if (m < k)
      swap(m, k);
    ll j = 1, ans = 0;
    for (int i = 0; i <= m + k; i++) {
      ans = (ans + C(n + i, n) * pow3[m + k - i] % MOD * j) % MOD;
      if (i < k)
        j = j * 2ll % MOD;
      else if (i < m)
        j = (j * 2ll - C(i, k)) % MOD;
      else
        j = (j * 2ll - C(i, k) - C(i, m)) % MOD;
    }
    ans = (ans + MOD) % MOD;
    PF("%lld\n", ans);
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define SF scanf
#define PF printf
#define MAXN 900010
#define MOD 1000000007
using namespace std;
typedef long long ll;
ll inv[MAXN], fac[MAXN], pow3[MAXN];
ll fsp(ll x, int y) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    y >>= 1;
  }
  return res;
}
ll C(int x, int y) { return fac[x] * inv[y] % MOD * inv[x - y] % MOD; }
int main() {
  pow3[0] = 1;
  fac[0] = 1;
  for (int i = 1; i <= 900000; i++) {
    pow3[i] = pow3[i - 1] * 3ll % MOD;
    fac[i] = fac[i - 1] * i % MOD;
  }
  inv[900000] = fsp(fac[900000], MOD - 2);
  for (int i = 900000; i > 0; i--)
    inv[i - 1] = inv[i] * i % MOD;
  int n, m, k;
  while (SF("%d%d%d", &n, &m, &k) != EOF) {
    n--;
    if (m < k)
      swap(m, k);
    ll j = 1, ans = 0;
    for (int i = 0; i <= m + k; i++) {
      ans = (ans + C(n + i, n) * pow3[m + k - i] % MOD * j) % MOD;
      if (i < k)
        j = j * 2ll % MOD;
      else if (i < m)
        j = (j * 2ll - C(i, k)) % MOD;
      else
        j = (j * 2ll - C(i, k) - C(i, m)) % MOD;
    }
    ans = (ans + MOD) % MOD;
    PF("%lld\n", ans);
  }
}
Problem p04004 (C++)

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

constexpr int mod = 1e9 + 7;

struct ModCombination {
  ModCombination(unsigned int n) : n(n), F(n + 1, 1), I(n + 1, 1) {
    for (int i = 1; i <= n; ++i)
      F[i] = 1ll * i * F[i - 1] % mod;
    for (int64_t i = mod - 2, j = F[n]; i; i >>= 1) {
      if (i & 1)
        I[n] = I[n] * j % mod;
      j = j * j % mod;
    }
    for (int i = n - 1; i; --i)
      I[i] = I[i + 1] * (i + 1ll) % mod;
  }

  unsigned int n;
  vector<int> F, I;

  int operator()(int p, int k) {
    if (p > n)
      cerr << "errored by ModCombination's operator()";
    if (k < 0 || k > p)
      return 0;
    return 1ll * F[p] * I[k] % mod * I[p - k] % mod;
  }
};

int mod_pow(int64_t a, int x) {
  int64_t res = 1;
  a %= mod;
  for (int i = 1; i <= x; i <<= 1) {
    if (i & x)
      (res *= a) %= mod;
    (a *= a) %= mod;
  }

  return res;
}

int main() {
  int N, M, K;
  cin >> N >> M >> K;

  ModCombination mC(N + M + K + 1);

  int64_t ans = 0;
  for (int m = 0; m <= M; ++m) {
    for (int k = 0; k <= K; ++k) {
      (ans += 1ll * mC(N + m + k - 1, m) * mC(N + k - 1, k) % mod *
              mod_pow(3, M - m + K - k) % mod) %= mod;
    }
  }

  cout << ans << endl;
}


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

constexpr int mod = 1e9 + 7;

struct ModCombination {
  ModCombination(unsigned int n) : n(n), F(n + 1, 1), I(n + 1, 1) {
    for (int i = 1; i <= n; ++i)
      F[i] = 1ll * i * F[i - 1] % mod;
    for (int64_t i = mod - 2, j = F[n]; i; i >>= 1) {
      if (i & 1)
        I[n] = I[n] * j % mod;
      j = j * j % mod;
    }
    for (int i = n - 1; i; --i)
      I[i] = I[i + 1] * (i + 1ll) % mod;
  }

  unsigned int n;
  vector<int> F, I;

  int operator()(int p, int k) {
    if (p > n)
      cerr << "errored by ModCombination's operator()";
    if (k < 0 || k > p)
      return 0;
    return 1ll * F[p] * I[k] % mod * I[p - k] % mod;
  }
};

int mod_pow(int64_t a, int x) {
  int64_t res = 1;
  a %= mod;
  for (int i = 1; i <= x; i <<= 1) {
    if (i & x)
      (res *= a) %= mod;
    (a *= a) %= mod;
  }

  return res;
}

int main() {
  int N, M, K;
  cin >> N >> M >> K;

  ModCombination mC(N + M + K + 1);

  int64_t ans = 0, sum = 1;
  for (int i = N - 1; i <= N + M + K - 1; ++i) {
    (ans += 1ll * mC(i, N - 1) * mod_pow(3, N + M + K - 1 - i) % mod * sum %
            mod) %= mod;
    sum =
        (2 * sum % mod -
         (mC(i - (N - 1), i - (N - 1) - M) + mC(i - (N - 1), K)) % mod + mod) %
        mod;
  }

  cout << ans << endl;
}
Problem p04004 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define mod 1000000007
#define N 300010
ll fac[N], inv[N], ivf[N];
void initmo() {
  fac[0] = ivf[0] = inv[1] = 1;
  for (ll i = 1; i < N; i++) {
    if (i > 1)
      inv[i] = (mod - mod / i * inv[mod % i] % mod) % mod;
    fac[i] = fac[i - 1] * i % mod;
    ivf[i] = ivf[i - 1] * inv[i] % mod;
  }
}
ll C(ll n, ll k) { return fac[n] * ivf[n - k] % mod * ivf[k] % mod; }
ll po(ll x, ll y) {
  ll res = 1;
  for (; y; y >>= 1) {
    if (y & 1)
      res = res * x % mod;
    x = x * x % mod;
  }
  return res;
}
int main() {
  initmo();
  ll a, b, c;
  cin >> a >> b >> c;
  if (a > N || b > N || c > N)
    return 0;
  a--;
  if (b > c)
    swap(b, c);
  ll ans = 0, sum = 1;
  for (ll k = a; k <= a + b + c; k++) {
    ll cur = po(3, a + b + c - k) * C(k, a) % mod;
    cur = cur * sum % mod;
    ans = (ans + cur) % mod;
    ll s = k - a;
    sum = sum * 2 % mod;
    if (b <= s)
      sum -= C(s, b);
    if (c <= s)
      sum -= C(s, s - c);
  }
  if (ans < 0)
    ans += mod;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define mod 1000000007
#define N 1000010
ll fac[N], inv[N], ivf[N];
void initmo() {
  fac[0] = ivf[0] = inv[1] = 1;
  for (ll i = 1; i < N; i++) {
    if (i > 1)
      inv[i] = (mod - mod / i * inv[mod % i] % mod) % mod;
    fac[i] = fac[i - 1] * i % mod;
    ivf[i] = ivf[i - 1] * inv[i] % mod;
  }
}
ll C(ll n, ll k) { return fac[n] * ivf[n - k] % mod * ivf[k] % mod; }
ll po(ll x, ll y) {
  ll res = 1;
  for (; y; y >>= 1) {
    if (y & 1)
      res = res * x % mod;
    x = x * x % mod;
  }
  return res;
}
int main() {
  initmo();
  ll a, b, c;
  cin >> a >> b >> c;
  if (a > N || b > N || c > N)
    return 0;
  a--;
  if (b > c)
    swap(b, c);
  ll ans = 0, sum = 1;
  for (ll k = a; k <= a + b + c; k++) {
    ll cur = po(3, a + b + c - k) * C(k, a) % mod;
    cur = cur * sum % mod;
    ans = (ans + cur) % mod;
    ll s = k - a;
    sum = sum * 2 % mod;
    if (b <= s)
      sum -= C(s, b);
    if (c <= s)
      sum -= C(s, s - c);
  }
  if (ans < 0)
    ans += mod;
  cout << ans << endl;
  return 0;
}
Problem p04004 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
ll quick(ll a, ll b) {
  ll ret = 1;
  while (b) {
    if (b & 1)
      ret = ret * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ret;
}
ll inv(ll a) { return quick(a, mod - 2); }
const int maxn = 3e5 + 7;
ll fac[maxn];
void db() {
  fac[0] = 1;
  for (int i = 1; i < maxn; i++)
    fac[i] = fac[i - 1] * i % mod;
}
ll C(int n, int m) {
  return fac[n] * inv(fac[n - m]) % mod * inv(fac[m]) % mod;
}
int main() {
  db();
  int n, m, k;
  cin >> n >> m >> k;
  ll ans = 0;
  ll cur = 1;
  for (int t = 0; t <= m + k; t++) {
    ans = (ans + cur * C(t + n - 1, n - 1) % mod * quick(3, m + k - t) % mod) %
          mod;
    cur = cur * 2 % mod;
    if (t + 1 > m)
      cur = (cur - C(t, m) + mod) % mod;
    if (t + 1 > k)
      cur = (cur - C(t, t - k) + mod) % mod;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
ll quick(ll a, ll b) {
  ll ret = 1;
  while (b) {
    if (b & 1)
      ret = ret * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ret;
}
ll inv(ll a) { return quick(a, mod - 2); }
const int maxn = 1e6 + 7;
ll fac[maxn];
void db() {
  fac[0] = 1;
  for (int i = 1; i < maxn; i++)
    fac[i] = fac[i - 1] * i % mod;
}
ll C(int n, int m) {
  return fac[n] * inv(fac[n - m]) % mod * inv(fac[m]) % mod;
}
int main() {
  db();
  int n, m, k;
  cin >> n >> m >> k;
  ll ans = 0;
  ll cur = 1;
  for (int t = 0; t <= m + k; t++) {
    ans = (ans + cur * C(t + n - 1, n - 1) % mod * quick(3, m + k - t) % mod) %
          mod;
    cur = cur * 2 % mod;
    if (t + 1 > m)
      cur = (cur - C(t, m) + mod) % mod;
    if (t + 1 > k)
      cur = (cur - C(t, t - k) + mod) % mod;
  }
  cout << ans << endl;
}
Problem p04004 (C++)

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

const int Mod = 1e9 + 7;
const int N = 9e5;

int n, m, k;
long long fac[N], vfac[N];

long long mpow(long long a, int b) {
  long long rt;
  for (rt = 1; b; b >>= 1, a = (a * a) % Mod)
    if (b & 1)
      rt = (rt * a) % Mod;
  return rt;
}
void init(int n) {
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = (fac[i - 1] * i) % Mod;
  vfac[n] = mpow(fac[n], Mod - 2);
  for (int i = n - 1; i >= 0; i--)
    vfac[i] = vfac[i + 1] * (i + 1) % Mod;
}
long long comb(int n, int m) {
  return fac[n] * vfac[n - m] % Mod * vfac[m] % Mod;
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  if (m < k)
    swap(m, k);
  init(m + k + n);
  long long sum = 0, ans = 0;
  for (int s = 0; s <= m + k; s++) {
    if (s == 0) {
      sum = 1;
    } else if (s <= k) {
      sum = (sum << 1) % Mod;
    } else if (s <= m) {
      sum = ((sum << 1) + Mod - comb(s - 1, s - k - 1)) % Mod;
    } else {
      sum = ((sum << 1) + Mod - comb(s - 1, s - k - 1) + Mod - comb(s - 1, m)) %
            Mod;
    }
    long long sub =
        sum * comb(n - 1 + s, n - 1) % Mod * mpow(3, m + k - s) % Mod;
    ans = (ans + sub) % Mod;
    //		printf( "s = %d sub = %lld\n", s, sub );
  }
  printf("%lld\n", ans);
}


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

const int Mod = 1e9 + 7;
const int N = 9e5 + 10;

int n, m, k;
long long fac[N], vfac[N];

long long mpow(long long a, int b) {
  long long rt;
  for (rt = 1; b; b >>= 1, a = (a * a) % Mod)
    if (b & 1)
      rt = (rt * a) % Mod;
  return rt;
}
void init(int n) {
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = (fac[i - 1] * i) % Mod;
  vfac[n] = mpow(fac[n], Mod - 2);
  for (int i = n - 1; i >= 0; i--)
    vfac[i] = vfac[i + 1] * (i + 1) % Mod;
}
long long comb(int n, int m) {
  return fac[n] * vfac[n - m] % Mod * vfac[m] % Mod;
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  if (m < k)
    swap(m, k);
  init(m + k + n);
  long long sum = 0, ans = 0;
  for (int s = 0; s <= m + k; s++) {
    if (s == 0) {
      sum = 1;
    } else if (s <= k) {
      sum = (sum << 1) % Mod;
    } else if (s <= m) {
      sum = ((sum << 1) + Mod - comb(s - 1, s - k - 1)) % Mod;
    } else {
      sum = ((sum << 1) + Mod - comb(s - 1, s - k - 1) + Mod - comb(s - 1, m)) %
            Mod;
    }
    long long sub =
        sum * comb(n - 1 + s, n - 1) % Mod * mpow(3, m + k - s) % Mod;
    ans = (ans + sub) % Mod;
    //		printf( "s = %d sub = %lld\n", s, sub );
  }
  printf("%lld\n", ans);
}
Problem p04004 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
#define N 2000005
#define ll long long
#define mod 1000000007
int fac[N], inv[N], t[N], a, b, c, mx, ans;
int q_pow(int x, int n) {
  int ret = 1;
  for (; n; n >>= 1, x = (ll)x * x % mod)
    if (n & 1)
      ret = (ll)ret * x % mod;
  return ret;
}
int C(int n, int m) { return (ll)fac[n] * inv[m] % mod * inv[n - m] % mod; }
int main() {
  // freopen("8102.in","r",stdin);
  // freopen("8102.out","w",stdout);
  mx = 2000000;
  t[0] = fac[0] = 1;
  for (int i = 1; i <= mx; i++)
    fac[i] = (ll)i * fac[i - 1] % mod, t[i] = (ll)t[i - 1] * 3 % mod;
  inv[mx] = q_pow(fac[mx], mod - 2);
  for (int i = mx; i; i--)
    inv[i - 1] = (ll)inv[i] * i % mod;
  // int T;scanf("%d",&T);
  // while(T--)
  // {
  scanf("%d%d%d", &a, &b, &c);
  ans = 0;
  for (int i = 0; i <= b; i++)
    for (int j = 0; j <= c; j++)
      ans = (ans + (ll)C(a + i - 1, i) * C(a + i + j - 1, j) % mod *
                       t[b + c - i - j]) %
            mod;
  printf("%d\n", ans);
  // }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
#define N 2000005
#define ll long long
#define mod 1000000007
int fac[N], inv[N], t[N], a, b, c, mx, ans;
int q_pow(int x, int n) {
  int ret = 1;
  for (; n; n >>= 1, x = (ll)x * x % mod)
    if (n & 1)
      ret = (ll)ret * x % mod;
  return ret;
}
int C(int n, int m) { return (ll)fac[n] * inv[m] % mod * inv[n - m] % mod; }
int main() {
  // freopen("8102.in","r",stdin);
  // freopen("8102.out","w",stdout);
  mx = 2000000;
  t[0] = fac[0] = 1;
  for (int i = 1; i <= mx; i++)
    fac[i] = (ll)i * fac[i - 1] % mod, t[i] = (ll)t[i - 1] * 3 % mod;
  inv[mx] = q_pow(fac[mx], mod - 2);
  for (int i = mx; i; i--)
    inv[i - 1] = (ll)inv[i] * i % mod;
  // int T;scanf("%d",&T);
  // while(T--)
  // {
  scanf("%d%d%d", &a, &b, &c);
  ans = 0;
  int rb = 0, rc = 0;
  for (int i = 0; i <= b + c; i++) {
    int tmp = (ll)t[b + c - i] * fac[a + i - 1] % mod, w = q_pow(2, i);
    if (i > b) {
      rb = (rb * 2) % mod;
      rb = (rb + C(i - 1, b)) % mod;
      w = (w - rb) % mod;
    }
    if (i > c) {
      rc = (rc * 2) % mod;
      rc = (rc + C(i - 1, c)) % mod;
      w = (w - rc) % mod;
    }
    w = ((ll)w * inv[i]) % mod;
    ans = (ans + (ll)w * tmp) % mod;
  }
  printf("%lld\n", (ll)(ans + mod) * inv[a - 1] % mod);
  // }
}
Problem p04004 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define MOD 1000000007
#define N 100000
using namespace std;
typedef long long LL;
LL mod_pow(LL base, LL exp) {
  if (exp == 0)
    return 1;
  if (exp & 1)
    return (mod_pow(base, exp - 1) * base) % MOD;
  else
    return mod_pow((base * base) % MOD, exp / 2);
}
LL fac[3 * N + 1];
LL fac_inv[3 * N + 1];
LL C(LL n, LL k) {
  if (k < 0 || n < k)
    return 0;
  LL num = fac[n];
  LL den = (fac_inv[n - k] * fac_inv[k]) % MOD;
  return (num * den) % MOD;
}
int main() {
  fac[0] = 1;
  for (LL n = 1; n <= 3 * N; n++)
    fac[n] = (fac[n - 1] * n) % MOD;
  fac_inv[3 * N] = mod_pow(fac[3 * N], MOD - 2);
  for (LL n = 3 * N - 1; n >= 0; n--)
    fac_inv[n] = (fac_inv[n + 1] * (n + 1)) % MOD;

  LL a, b, c;
  cin >> a >> b >> c;
  a++;
  // WLOG, b > c
  if (!(b > c)) {
    b = b ^ c;
    c = b ^ c;
    b = b ^ c;
  }

  LL ans = 0;
  LL sum = 1;
  LL pull = 0;
  for (LL n = a; n <= a + b + c; n++) {
    LL sub = 0;

    sub = (sub + sum - pull) % MOD;
    sub = (sub * C(n - 2, a - 2)) % MOD;
    sub = (sub * mod_pow(3, a + b + c - n)) % MOD;
    ans = (ans + sub) % MOD;

    sum = (2 * sum - C(n - a, c)) % MOD;
    pull = (2 * pull + C(n - a, n - a - b)) % MOD;
  }
  cout << (ans + MOD) % MOD << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#define MOD 1000000007
#define N 300000
using namespace std;
typedef long long LL;
LL mod_pow(LL base, LL exp) {
  if (exp == 0)
    return 1;
  if (exp & 1)
    return (mod_pow(base, exp - 1) * base) % MOD;
  else
    return mod_pow((base * base) % MOD, exp / 2);
}
LL fac[3 * N + 1];
LL fac_inv[3 * N + 1];
LL C(LL n, LL k) {
  if (k < 0 || n < k)
    return 0;
  LL num = fac[n];
  LL den = (fac_inv[n - k] * fac_inv[k]) % MOD;
  return (num * den) % MOD;
}
int main() {
  fac[0] = 1;
  for (LL n = 1; n <= 3 * N; n++)
    fac[n] = (fac[n - 1] * n) % MOD;
  fac_inv[3 * N] = mod_pow(fac[3 * N], MOD - 2);
  for (LL n = 3 * N - 1; n >= 0; n--)
    fac_inv[n] = (fac_inv[n + 1] * (n + 1)) % MOD;

  LL a, b, c;
  cin >> a >> b >> c;
  a++;
  // WLOG, b > c
  if (!(b > c)) {
    b = b ^ c;
    c = b ^ c;
    b = b ^ c;
  }

  LL ans = 0;
  LL sum = 1;
  LL pull = 0;
  for (LL n = a; n <= a + b + c; n++) {
    LL sub = 0;

    sub = (sub + sum - pull) % MOD;
    sub = (sub * C(n - 2, a - 2)) % MOD;
    sub = (sub * mod_pow(3, a + b + c - n)) % MOD;
    ans = (ans + sub) % MOD;

    sum = (2 * sum - C(n - a, c)) % MOD;
    pull = (2 * pull + C(n - a, n - a - b)) % MOD;
  }
  cout << (ans + MOD) % MOD << endl;

  return 0;
}
Problem p04004 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>
#include <string.h>

const int kN = 300000 + 5;
const int MOD = (int)1e9 + 7;
int Inv[kN], Finv[kN], F[kN], n, m, k;
int pascal[kN];

int binom(int a, int b) {
  if (b < 0 || b > a)
    return 0;
  return F[a] * 1LL * Finv[b] % MOD * Finv[a - b] % MOD;
}

inline void add(int &a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
}

int power(int a, int b, int p) {
  int ret = 1;
  for (; b; b >>= 1, a = a * 1LL * a % MOD)
    if (b & 1)
      ret = ret * 1LL * a % MOD;
  return ret;
}

int main() {
  Inv[1] = 1;
  for (int i = 2; i < kN; ++i)
    Inv[i] = (MOD - MOD / i) * 1LL * Inv[MOD % i] % MOD;
  F[0] = Finv[0] = 1;
  for (int i = 1; i < kN; ++i) {
    F[i] = F[i - 1] * 1LL * i % MOD;
    Finv[i] = Finv[i - 1] * 1LL * Inv[i] % MOD;
  }
  scanf("%d%d%d", &n, &m, &k);
  pascal[0] = 1;
  for (int i = 1; i <= m + k; ++i) {
    pascal[i] = (pascal[i - 1] << 1) % MOD;
    if (i >= m + 1)
      add(pascal[i], MOD - binom(i - 1, m));
    if (i >= k + 1)
      add(pascal[i], MOD - binom(i - 1, i - (k + 1)));
  }
  int result = 0;
  int t = n + m + k;
  for (int i = n; i <= t; ++i) {
    add(result, binom(i - 1, n - 1) * 1LL * power(3, t - i, MOD) % MOD *
                    pascal[i - n] % MOD);
  }
  printf("%d\n", result);
}


Fixed submission:
#include <algorithm>
#include <stdio.h>
#include <string.h>

const int kN = 1000000 + 5;
const int MOD = (int)1e9 + 7;
int Inv[kN], Finv[kN], F[kN], n, m, k;
int pascal[kN];

int binom(int a, int b) {
  if (b < 0 || b > a)
    return 0;
  return F[a] * 1LL * Finv[b] % MOD * Finv[a - b] % MOD;
}

inline void add(int &a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
}

int power(int a, int b, int p) {
  int ret = 1;
  for (; b; b >>= 1, a = a * 1LL * a % MOD)
    if (b & 1)
      ret = ret * 1LL * a % MOD;
  return ret;
}

int main() {
  Inv[1] = 1;
  for (int i = 2; i < kN; ++i)
    Inv[i] = (MOD - MOD / i) * 1LL * Inv[MOD % i] % MOD;
  F[0] = Finv[0] = 1;
  for (int i = 1; i < kN; ++i) {
    F[i] = F[i - 1] * 1LL * i % MOD;
    Finv[i] = Finv[i - 1] * 1LL * Inv[i] % MOD;
  }
  scanf("%d%d%d", &n, &m, &k);
  pascal[0] = 1;
  for (int i = 1; i <= m + k; ++i) {
    pascal[i] = (pascal[i - 1] << 1) % MOD;
    if (i >= m + 1)
      add(pascal[i], MOD - binom(i - 1, m));
    if (i >= k + 1)
      add(pascal[i], MOD - binom(i - 1, i - (k + 1)));
  }
  int result = 0;
  int t = n + m + k;
  for (int i = n; i <= t; ++i) {
    add(result, binom(i - 1, n - 1) * 1LL * power(3, t - i, MOD) % MOD *
                    pascal[i - n] % MOD);
  }
  printf("%d\n", result);
}
Problem p04004 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const int MAXN = 5005;
const int mod = 1e9 + 7;

lint ipow(int x, int p) {
  lint ret = 1, piv = x;
  while (p) {
    if (p & 1)
      ret = ret * piv % mod;
    piv = piv * piv % mod;
    p >>= 1;
  }
  return ret;
}

int n, m, k;
lint fact[MAXN], invf[MAXN];

lint bino(int x, int y) {
  return fact[x] * (invf[y] * invf[x - y] % mod) % mod;
}

int main() {
  fact[0] = invf[0] = 1;
  for (int i = 1; i < MAXN; i++) {
    fact[i] = fact[i - 1] * i % mod;
    invf[i] = ipow(fact[i], mod - 2);
  }
  cin >> n >> m >> k;
  lint func = 0;
  for (int j = 0; j <= k; j++) {
    func += bino(n - 1 + j, j) * ipow(3, mod - 1 - j) % mod;
    func %= mod;
  }
  lint ret = func * ipow(3, m + k) % mod;
  for (int i = 1; i <= m; i++) {
    lint aux = bino(n - 1 + i + k, k) * ipow(3, mod - 1 - k) % mod;
    func += mod - aux;
    func *= 3ll * (mod + 1) / 2;
    func += aux;
    func %= mod;
    lint base = ipow(3, m + k - i) * bino(n - 1 + i, i) % mod;
    ret += base * func % mod;
    ret %= mod;
  }
  cout << ret << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const int MAXN = 1000005;
const int mod = 1e9 + 7;

lint ipow(int x, int p) {
  lint ret = 1, piv = x;
  while (p) {
    if (p & 1)
      ret = ret * piv % mod;
    piv = piv * piv % mod;
    p >>= 1;
  }
  return ret;
}

int n, m, k;
lint fact[MAXN], invf[MAXN];

lint bino(int x, int y) {
  return fact[x] * (invf[y] * invf[x - y] % mod) % mod;
}

int main() {
  fact[0] = invf[0] = 1;
  for (int i = 1; i < MAXN; i++) {
    fact[i] = fact[i - 1] * i % mod;
    invf[i] = ipow(fact[i], mod - 2);
  }
  cin >> n >> m >> k;
  lint func = 0;
  for (int j = 0; j <= k; j++) {
    func += bino(n - 1 + j, j) * ipow(3, mod - 1 - j) % mod;
    func %= mod;
  }
  lint ret = func * ipow(3, m + k) % mod;
  for (int i = 1; i <= m; i++) {
    lint aux = bino(n - 1 + i + k, k) * ipow(3, mod - 1 - k) % mod;
    func += mod - aux;
    func *= 3ll * (mod + 1) / 2;
    func += aux;
    func %= mod;
    lint base = ipow(3, m + k - i) * bino(n - 1 + i, i) % mod;
    ret += base * func % mod;
    ret %= mod;
  }
  cout << ret << endl;
}
Problem p04004 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
const int MAXN = 6e5 + 10, MOD = 1e9 + 7;
int fac[MAXN], ifac[MAXN], f3[MAXN];
void init() {
  fac[0] = fac[1] = 1;
  for (int i = 2; i < MAXN; ++i)
    fac[i] = 1ll * fac[i - 1] * i % MOD;
  ifac[0] = ifac[1] = 1;
  for (int i = 2; i < MAXN; ++i)
    ifac[i] = MOD - 1ll * ifac[MOD % i] * (MOD / i) % MOD;
  for (int i = 2; i < MAXN; ++i)
    ifac[i] = 1ll * ifac[i - 1] * ifac[i] % MOD;
  f3[0] = 1;
  for (int i = 1; i < MAXN; ++i)
    f3[i] = 1ll * f3[i - 1] * 3 % MOD;
}

int C(int a, int b) {
  if (b < 0 || b > a)
    return 0;
  return 1ll * fac[a] * ifac[b] % MOD * ifac[a - b] % MOD;
}
int n, m, k;
int main() {
  init();
  cin >> n >> m >> k;
  int ans = 0, last = 1, st = 0, ed = 0;
  for (int i = 0; i <= m + k; ++i) {
    ans = (ans + 1ll * last * f3[m + k - i] % MOD * C(n + i - 1, n - 1)) % MOD;
    last = 2ll * last % MOD;
    last = (last + C(i, st - 1)) % MOD;
    last = (last + MOD - C(i, ed)) % MOD;
    if (i - st >= m) {
      last = (last + MOD - C(i + 1, st)) % MOD;
      st++;
    }
    if (ed < k) {
      ed++;
      last = (last + C(i + 1, ed)) % MOD;
    }
    if (st > ed)
      break;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
const int MAXN = 9e5 + 10, MOD = 1e9 + 7;
int fac[MAXN], ifac[MAXN], f3[MAXN];
void init() {
  fac[0] = fac[1] = 1;
  for (int i = 2; i < MAXN; ++i)
    fac[i] = 1ll * fac[i - 1] * i % MOD;
  ifac[0] = ifac[1] = 1;
  for (int i = 2; i < MAXN; ++i)
    ifac[i] = MOD - 1ll * ifac[MOD % i] * (MOD / i) % MOD;
  for (int i = 2; i < MAXN; ++i)
    ifac[i] = 1ll * ifac[i - 1] * ifac[i] % MOD;
  f3[0] = 1;
  for (int i = 1; i < MAXN; ++i)
    f3[i] = 1ll * f3[i - 1] * 3 % MOD;
}

int C(int a, int b) {
  if (b < 0 || b > a)
    return 0;
  return 1ll * fac[a] * ifac[b] % MOD * ifac[a - b] % MOD;
}
int n, m, k;
int main() {
  init();
  cin >> n >> m >> k;
  int ans = 0, last = 1, st = 0, ed = 0;
  for (int i = 0; i <= m + k; ++i) {
    ans = (ans + 1ll * last * f3[m + k - i] % MOD * C(n + i - 1, n - 1)) % MOD;
    last = 2ll * last % MOD;
    last = (last + C(i, st - 1)) % MOD;
    last = (last + MOD - C(i, ed)) % MOD;
    if (i - st >= m) {
      last = (last + MOD - C(i + 1, st)) % MOD;
      st++;
    }
    if (ed < k) {
      ed++;
      last = (last + C(i + 1, ed)) % MOD;
    }
    if (st > ed)
      break;
  }
  cout << ans << endl;
  return 0;
}
Problem p04004 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MAXN = 1e6 + 5;
const ll p = 1e9 + 7;
const ll inv3 = (p + 1) / 3;

ll fact[MAXN], invfact[MAXN], inv[MAXN], n, m, k, c[MAXN];
ll p3[MAXN], pinv3[MAXN];

ll comb(int x, int y) {
  if (x < 0 || y < 0 || x < y)
    return 0;
  return fact[x] * invfact[y] % p * invfact[x - y] % p;
}

ll exp(ll x, ll k) {
  if (k == 0)
    return 1;
  ll y = exp(x, k / 2);
  return (k % 2 == 0) ? y * y % p : y * y % p * x % p;
}

ll inve(ll x) { return exp(x, p - 2); }

void init() {
  inv[1] = fact[0] = fact[1] = invfact[0] = invfact[1] = 1;
  for (int i = 2; i < MAXN; ++i) {
    inv[i] = (p - (p / i) * inv[p % i] % p) % p;
    fact[i] = i * fact[i - 1] % p;
    invfact[i] = inv[i] * invfact[i - 1] % p;
  }

  p3[0] = pinv3[0] = 1;
  for (int i = 1; i < MAXN; ++i)
    p3[i] = 3 * p3[i - 1] % p, pinv3[i] = inv3 * pinv3[i - 1] % p;
}

int main() {
  cin >> n >> m >> k;
  init();
  for (int i = 0; i <= k; ++i)
    c[0] = (c[0] + pinv3[i]) % p;
  for (int i = 1; i < MAXN; ++i)
    c[i] = 3 * (p + 1) / 2 % p *
           (c[i - 1] + p - comb(i + k, k) * pinv3[k + 1] % p) % p;
  ll ans = 0;
  for (int i = 0; i <= m; ++i)
    ans = (ans + comb(n - 1 + i, i) * pinv3[i] % p * c[n - 1 + i] % p) % p;
  cout << ans * p3[m + k] % p << endl;
}

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

const int MAXN = 1e6 + 5;
const ll p = 1e9 + 7;
const ll inv3 = (p + 1) / 3;

ll fact[MAXN], invfact[MAXN], inv[MAXN], n, m, k, c[MAXN];
ll p3[MAXN], pinv3[MAXN];

ll comb(int x, int y) {
  if (x < 0 || y < 0 || x < y)
    return 0;
  return fact[x] * invfact[y] % p * invfact[x - y] % p;
}

ll exp(ll x, ll k) {
  if (k == 0)
    return 1;
  ll y = exp(x, k / 2);
  return (k % 2 == 0) ? y * y % p : y * y % p * x % p;
}

ll inve(ll x) { return exp(x, p - 2); }

void init() {
  inv[1] = fact[0] = fact[1] = invfact[0] = invfact[1] = 1;
  for (int i = 2; i < MAXN; ++i) {
    inv[i] = (p - (p / i) * inv[p % i] % p) % p;
    fact[i] = i * fact[i - 1] % p;
    invfact[i] = inv[i] * invfact[i - 1] % p;
  }

  p3[0] = pinv3[0] = 1;
  for (int i = 1; i < MAXN; ++i)
    p3[i] = 3 * p3[i - 1] % p, pinv3[i] = inv3 * pinv3[i - 1] % p;
}

int main() {
  cin >> n >> m >> k;
  init();
  for (int i = 0; i <= k; ++i)
    c[0] = (c[0] + pinv3[i]) % p;
  for (int i = 1; i < MAXN - 300000; ++i)
    c[i] = 3 * (p + 1) / 2 % p *
           (c[i - 1] + p - comb(i + k, k) * pinv3[k + 1] % p) % p;
  ll ans = 0;
  for (int i = 0; i <= m; ++i)
    ans = (ans + comb(n - 1 + i, i) * pinv3[i] % p * c[n - 1 + i] % p) % p;
  cout << ans * p3[m + k] % p << endl;
}
Problem p04004 (C++)

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

const int MOD = (int)1e9 + 7;
const int N = 6e5 + 7;
long long fact[N], invfact[N];

long long power(long long a, long long n) {
  long long res = 1;
  while (n != 0) {
    if (n % 2 != 0)
      res = res * a % MOD;
    a = a * a % MOD;
    n /= 2;
  }
  return res;
}

long long inverse(int a) { return power(a, MOD - 2); }

long long c(int n, int k) {
  if (k < 0 || k > n)
    return 0;
  return fact[n] * invfact[k] % MOD * invfact[n - k] % MOD;
}

int main() {
  ios::sync_with_stdio(false);
  fact[0] = 1;
  for (int i = 1; i < N; ++i)
    fact[i] = fact[i - 1] * i % MOD;
  for (int i = 0; i < N; ++i)
    invfact[i] = inverse(fact[i]);
  int n, m, k;
  cin >> n >> m >> k;
  int lft = 0, rgt = -1;
  long long sum = 0, res = 0;
  for (int l = 0; l <= m + k; ++l) {
    int newLft = l - min(l, k);
    int newRgt = min(l, m);
    while (rgt < newRgt)
      sum += c(l, ++rgt);
    while (rgt > newRgt)
      sum -= c(l, rgt--);
    while (lft < newLft)
      sum -= c(l, lft++);
    while (lft > newLft)
      sum += c(l, --lft);
    sum %= MOD;
    res = (res + sum * power(3, m + k - l) % MOD * c(n + l - 1, n - 1)) % MOD;
    sum = (sum * 2 - c(l, lft++) - c(l, rgt)) % MOD;
  }
  if (res < 0)
    res += MOD;
  cout << res << endl;
  return 0;
}


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

const int MOD = (int)1e9 + 7;
const int N = 9e5 + 7;
long long fact[N], invfact[N];

long long power(long long a, long long n) {
  long long res = 1;
  while (n != 0) {
    if (n % 2 != 0)
      res = res * a % MOD;
    a = a * a % MOD;
    n /= 2;
  }
  return res;
}

long long inverse(int a) { return power(a, MOD - 2); }

long long c(int n, int k) {
  if (k < 0 || k > n)
    return 0;
  return fact[n] * invfact[k] % MOD * invfact[n - k] % MOD;
}

int main() {
  ios::sync_with_stdio(false);
  fact[0] = 1;
  for (int i = 1; i < N; ++i)
    fact[i] = fact[i - 1] * i % MOD;
  for (int i = 0; i < N; ++i)
    invfact[i] = inverse(fact[i]);
  int n, m, k;
  cin >> n >> m >> k;
  int lft = 0, rgt = -1;
  long long sum = 0, res = 0;
  for (int l = 0; l <= m + k; ++l) {
    int newLft = l - min(l, k);
    int newRgt = min(l, m);
    while (rgt < newRgt)
      sum += c(l, ++rgt);
    while (rgt > newRgt)
      sum -= c(l, rgt--);
    while (lft < newLft)
      sum -= c(l, lft++);
    while (lft > newLft)
      sum += c(l, --lft);
    sum %= MOD;
    res = (res + sum * power(3, m + k - l) % MOD * c(n + l - 1, n - 1)) % MOD;
    sum = (sum * 2 - c(l, lft++) - c(l, rgt)) % MOD;
  }
  if (res < 0)
    res += MOD;
  cout << res << endl;
  return 0;
}
Problem p04005 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll a, b, c;
ll ans = 1000000000000000;
int main(void) {
  cin >> a >> b >> c;
  for (int i = 1; i < a; i++) {
    ans = min(ans, abs(b * c * i - b * c * (a - i)));
  }
  for (int i = 1; i < b; i++) {
    ans = min(ans, abs(c * a * i - c * a * (b - i)));
  }
  for (int i = 1; i < c; i++) {
    ans = min(ans, abs(a * b * i - a * b * (c - i)));
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll a, b, c;
ll ans = 1000000000000000;
int main(void) {
  cin >> a >> b >> c;
  ans = min({abs(b * c * (a / 2) - b * c * (a - a / 2)),
             abs(c * a * (b / 2) - c * a * (b - b / 2)),
             abs(a * b * (c / 2) - a * b * (c - c / 2))});
  cout << ans << endl;
}
Problem p04005 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define INF 1e11
using namespace std;
typedef long long ll;

ll min(ll a, ll b) { return (a < b) ? a : b; }

int main() {
  ll a, b, c, diff = INF;
  cin >> a >> b >> c;
  loop(i, 1, a) diff = min(diff, abs(a - i - i) * b * c);
  loop(i, 1, b) diff = min(diff, abs(b - i - i) * c * a);
  loop(i, 1, c) diff = min(diff, abs(c - i - i) * a * b);
  cout << diff << endl;
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define INF 1e11
using namespace std;
typedef long long ll;

ll min(ll a, ll b) { return (a < b) ? a : b; }

int main() {
  ll a, b, c, diff = INF;
  cin >> a >> b >> c;
  diff = (a % 2) ? b * c : 0;
  diff = (b % 2) ? min(diff, c * a) : 0;
  diff = (c % 2) ? min(diff, a * b) : 0;
  cout << diff << endl;
  return 0;
}
Problem p04005 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define F first
#define S second
#define all(a) a.begin(), a.end()
#define setDP(arr) memset(arr, -1, sizeof arr)
#define Clear(arr) memset(arr, 0, sizeof arr)
#define oo 2000000000
#define inf 1000000000000000000
#define P1 31
#define P2 37
#define M 1000000007
#define M2 1000000009
#define pii pair<ll, ll>
typedef long long ll;
using namespace std;
const int N = 255001;
const int SQRTN = 320;
const int LOGN = 20;
const long double PI = acos(-1);
const long double TAU = 2 * PI;

ll a[3];

signed main() {
  cin >> a[0] >> a[1] >> a[2];
  if (!((a[0] & 1) && (a[1] & 1) && (a[2] & 1)))
    return puts("0");
  sort(a, a + 3);
  cout << a[0] * a[1] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define F first
#define S second
#define all(a) a.begin(), a.end()
#define setDP(arr) memset(arr, -1, sizeof arr)
#define Clear(arr) memset(arr, 0, sizeof arr)
#define oo 2000000000
#define inf 1000000000000000000
#define P1 31
#define P2 37
#define M 1000000007
#define M2 1000000009
#define pii pair<ll, ll>
typedef long long ll;
using namespace std;
const int N = 255001;
const int SQRTN = 320;
const int LOGN = 20;
const long double PI = acos(-1);
const long double TAU = 2 * PI;

ll a[3];

signed main() {
  cin >> a[0] >> a[1] >> a[2];
  if (!((a[0] & 1) && (a[1] & 1) && (a[2] & 1)))
    return puts("0"), 0;
  sort(a, a + 3);
  cout << a[0] * a[1] << endl;
}