Problem p03094 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
template <class T> inline void read(T &x) {
  x = 0;
  register char c = getchar();
  register bool f = 0;
  while (!isdigit(c))
    f ^= c == '-', c = getchar();
  while (isdigit(c))
    x = x * 10 + c - '0', c = getchar();
  if (f)
    x = -x;
}
const int N = 1e5 + 10, mod = 1e9 + 7;
int n, ans, f[N], g[N][3], fc[N], fi[N], vi[N];
inline int inc(int a, int b) { return (a += b) >= mod ? a - mod : a; }
inline int binom(int n, int m) {
  return (ll)fc[n] * fi[m] % mod * fi[n - m] % mod;
}
inline int fpow(int a, int b) {
  int s = 1;
  for (; b; b >>= 1, a = (ll)a * a % mod)
    if (b & 1)
      s = (ll)s * a % mod;
  return s;
}
int main() {
  std::cin >> n;
  g[1][0] = fi[0] = fi[1] = vi[0] = vi[1] = fc[0] = fc[1] = 1;
  for (int i = 2; i <= n; i++)
    fc[i] = (ll)fc[i - 1] * i % mod;
  for (int i = 2; i <= n; i++)
    vi[i] = (ll)(mod - mod / i) * vi[mod % i] % mod;
  for (int i = 2; i <= n; i++)
    fi[i] = (ll)fi[i - 1] * vi[i] % mod;
  for (int i = 2; i <= n + 1; i++) {
    g[i][0] = inc(g[i - 1][1], g[i - 1][2]);
    g[i][1] = inc(g[i - 1][0], g[i - 1][2]);
    g[i][2] = inc(g[i - 1][0], g[i - 1][1]);
    // printf("> %d %d %d\n",g[i][0],g[i][1],g[i][2]);
  }
  for (int i = 1; i <= n; i++)
    f[i] = (ll)g[i + 1][1] * binom(n, i) % mod;
  for (int i = 1; i <= n; i++)
    ans = (ans + (ll)f[i] * vi[i]) % mod;
  // for(int i=1;i<=n;i++)std::cout<<f[i]<<" \n"[i==n];
  // std::cout<<ans<<std::endl;
  std::cout << (ll)ans * fpow((ll)n * fpow(3, n) % mod, mod - 2) % mod
            << std::endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
template <class T> inline void read(T &x) {
  x = 0;
  register char c = getchar();
  register bool f = 0;
  while (!isdigit(c))
    f ^= c == '-', c = getchar();
  while (isdigit(c))
    x = x * 10 + c - '0', c = getchar();
  if (f)
    x = -x;
}
const int N = 1e6 + 10, mod = 1e9 + 7;
int n, ans, f[N], g[N][3], fc[N], fi[N], vi[N];
inline int inc(int a, int b) { return (a += b) >= mod ? a - mod : a; }
inline int binom(int n, int m) {
  return (ll)fc[n] * fi[m] % mod * fi[n - m] % mod;
}
inline int fpow(int a, int b) {
  int s = 1;
  for (; b; b >>= 1, a = (ll)a * a % mod)
    if (b & 1)
      s = (ll)s * a % mod;
  return s;
}
int main() {
  std::cin >> n;
  g[1][0] = fi[0] = fi[1] = vi[0] = vi[1] = fc[0] = fc[1] = 1;
  for (int i = 2; i <= n; i++)
    fc[i] = (ll)fc[i - 1] * i % mod;
  for (int i = 2; i <= n; i++)
    vi[i] = (ll)(mod - mod / i) * vi[mod % i] % mod;
  for (int i = 2; i <= n; i++)
    fi[i] = (ll)fi[i - 1] * vi[i] % mod;
  for (int i = 2; i <= n + 1; i++) {
    g[i][0] = inc(g[i - 1][1], g[i - 1][2]);
    g[i][1] = inc(g[i - 1][0], g[i - 1][2]);
    g[i][2] = inc(g[i - 1][0], g[i - 1][1]);
    // printf("> %d %d %d\n",g[i][0],g[i][1],g[i][2]);
  }
  for (int i = 1; i <= n; i++)
    f[i] = (ll)g[i + 1][1] * binom(n, i) % mod;
  for (int i = 1; i <= n; i++)
    ans = (ans + (ll)f[i] * vi[i]) % mod;
  // for(int i=1;i<=n;i++)std::cout<<f[i]<<" \n"[i==n];
  // std::cout<<ans<<std::endl;
  std::cout << (ll)ans * fpow((ll)n * fpow(3, n) % mod, mod - 2) % mod
            << std::endl;
}