Problem p04004 (C++)

Buggy submission (status: Time Limit Exceeded):
////////////////////////////////////////////
///                                      ///
///         Template ver. 1 rapel        ///
/// Fear is Temporary, Regret is Forever ///
///          Must Try and Get AC         ///
///                                      ///
////////////////////////////////////////////
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdio.h>

#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>

#include <complex>
#include <math.h>

#include <assert.h>
#include <time.h>
// #include <chrono>
// #include <random>

#include <bitset>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define FI first
#define SE second
#define MP make_pair
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define endl '\n'
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;

void desperate_optimization(int precision) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(precision);
}

const LL MOD = 1e9 + 7;
const int N = 1e6;

LL fct[N + 5], inv[N + 5];

LL fast(LL A, LL B) {
  LL res = 1;
  while (B) {
    if (B & 1)
      res = res * A % MOD;
    B /= 2;
    A = A * A % MOD;
  }
  return res;
}

void pre() {
  fct[0] = 1;
  for (int i = 1; i <= N; i++)
    fct[i] = fct[i - 1] * i % MOD;
  inv[N] = fast(fct[N], MOD - 2);
  for (int i = N - 1; i >= 0; i--)
    inv[i] = inv[i + 1] * (i + 1) % MOD;
}

LL combin(int n, int r) {
  if (n < r)
    return 0;
  return fct[n] * inv[r] % MOD * inv[n - r] % MOD;
}

void hora(int tc) {
  int n, m, k;
  cin >> n >> m >> k;
  int space = m + k;
  LL tot = 0;
  for (int i = 0; i <= m; i++) {
    for (int j = 0; j <= k; j++) {
      tot += combin(n - 1 + i + j, n - 1) * combin(i + j, j) % MOD *
             fast(3, m + k - i - j) % MOD;
      tot %= MOD;
    }
  }
  cout << tot << endl;
}

int main() {
  pre();
  desperate_optimization(10);
  int ntc = 1;
  // cin>>ntc;
  for (int tc = 1; tc <= ntc; tc++)
    hora(tc);
  return 0;
}


Fixed submission:
////////////////////////////////////////////
///                                      ///
///         Template ver. 1 rapel        ///
/// Fear is Temporary, Regret is Forever ///
///          Must Try and Get AC         ///
///                                      ///
////////////////////////////////////////////
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdio.h>

#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>

#include <complex>
#include <math.h>

#include <assert.h>
#include <time.h>
// #include <chrono>
// #include <random>

#include <bitset>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define FI first
#define SE second
#define MP make_pair
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define endl '\n'
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;

void desperate_optimization(int precision) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(precision);
}

const LL MOD = 1e9 + 7;
const int N = 1e6;

LL fct[N + 5], inv[N + 5];

LL fast(LL A, LL B) {
  LL res = 1;
  while (B) {
    if (B & 1)
      res = res * A % MOD;
    B /= 2;
    A = A * A % MOD;
  }
  return res;
}

void pre() {
  fct[0] = 1;
  for (int i = 1; i <= N; i++)
    fct[i] = fct[i - 1] * i % MOD;
  inv[N] = fast(fct[N], MOD - 2);
  for (int i = N - 1; i >= 0; i--)
    inv[i] = inv[i + 1] * (i + 1) % MOD;
}

LL combin(int n, int r) {
  if (n < r)
    return 0;
  return fct[n] * inv[r] % MOD * inv[n - r] % MOD;
}

void hora(int tc) {
  int n, m, k;
  cin >> n >> m >> k;
  int space = m + k;
  LL tot = 0;
  LL totals = 1;
  for (int w = 0; w <= m + k; w++) {
    if (w > m)
      totals -= combin(w - 1, m);
    if (w > k)
      totals -= combin(w - 1, k);
    totals %= MOD, totals += MOD, totals %= MOD;
    tot += combin(n - 1 + w, n - 1) * totals % MOD * fast(3, m + k - w) % MOD;
    totals = totals * 2 % MOD;
    tot %= MOD;
  }
  cout << tot << endl;
}

int main() {
  pre();
  desperate_optimization(10);
  int ntc = 1;
  // cin>>ntc;
  for (int tc = 1; tc <= ntc; tc++)
    hora(tc);
  return 0;
}