Problem p03199 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <ccomplex>
#include <clocale>
#include <cmath>
#include <complex>
#include <ctype.h>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <utility>
#include <valarray>
#include <vector>
#include <wchar.h>

using namespace std;
typedef long long int LL;
typedef long long int ll;
typedef pair<long long int, long long int> pii;
typedef pair<double, double> pdd;

#define SORT(c) sort((c).begin(), (c).end())
#define BACKSORT(c) sort((c).begin(), (c).end(), std::greater<LL>())
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

LL mod = 998244353;

bool find(map<pair<LL, LL>, bool> &dic, LL a, LL b) {
  auto iter = dic.find(make_pair(a, b));
  return iter != end(dic);
}

bool is1(map<pair<LL, LL>, bool> &dic, LL a, LL b) {
  auto iter = dic.find(make_pair(a, b));
  return iter->second;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  LL N, M;
  cin >> N >> M;

  map<pair<LL, LL>, bool> dic;

  REP(i, M) {
    LL a, b, c;
    cin >> a >> b >> c;
    dic.insert(make_pair(make_pair(a, b), c == 1));
  }

  LL now1 = 1;
  LL now0 = 1;

  if (find(dic, 1, 1)) {
    if (is1(dic, 1, 1)) {
      now0 = 0;
    } else {
      now1 = 0;
    }
  }

  REP(i, N - 1) {
    // i+2,i+2 ->i+3,i+1
    bool flag1 = true;
    bool flag0 = true;
    LL next1 = 0;
    LL next0 = 0;
    if (find(dic, i + 3, i + 1) && find(dic, i + 1, i + 3)) {
      if ((is1(dic, i + 3, i + 1) && is1(dic, i + 1, i + 3)) ||
          ((!is1(dic, i + 3, i + 1)) && (!is1(dic, i + 1, i + 3)))) {
        // kanarazu 0
        flag1 = false;
      } else {
        flag0 = false;
      }
    }
    LL notuse = 0;
    LL cnt = 0;
    if (find(dic, i + 2, i + 1)) {
      if (is1(dic, i + 2, i + 1)) {
        cnt++;
      }
    } else {
      notuse++;
    }
    if (find(dic, i + 1, i + 2)) {
      if (is1(dic, i + 1, i + 2)) {
        cnt++;
      }
    } else {
      notuse++;
    }
    if (flag0) {
      if (notuse == 0) {
        if (cnt % 2 == 0) {
          next0 += now0;
        } else {
          next0 += now1;
        }
      } else {
        next0 += (now0 + now1) * notuse;
      }
    }
    if (flag1) {
      if (notuse == 0) {
        if (cnt % 2 == 0) {
          next1 += now1;
        } else {
          next1 += now0;
        }
      } else {
        next1 += (now0 + now1) * notuse;
      }
    }
    if (i != N - 2) {
      if ((!find(dic, i + 3, i + 1)) && (!find(dic, i + 1, i + 3))) {
        next1 *= 2;
        next0 *= 2;
      }
    }
    if (find(dic, i + 2, i + 2)) {
      if (is1(dic, i + 2, i + 2)) {
        next0 = 0;
      } else {
        next1 = 0;
      }
    }
    next1 %= mod;
    next0 %= mod;
    now1 = next1;
    now0 = next0;
  }

  LL sum = (now1 + now0) % mod;

  LL c = 0;

  for (auto i = dic.begin(); i != dic.end(); ++i) {
    auto key = i->first;
    if (abs(key.first - key.second) >= 3) {
      if (find(dic, key.second, key.first)) {
        if (is1(dic, key.first, key.second) !=
            is1(dic, key.second, key.first)) {
          sum = 0;
        }
        if (key.first > key.second) {
          c++;
        }
      } else {
        c++;
      }
    }
  }

  LL a = -c;

  REP(i, N - 3) { a += i + 1; }

  while (a > 0) {
    if (a > 30) {
      a -= 30;
      sum *= 75497471;
      sum %= mod;
    } else {
      a--;
      sum *= 2;
      sum %= mod;
    }
  }

  cout << sum << endl;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <ccomplex>
#include <clocale>
#include <cmath>
#include <complex>
#include <ctype.h>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <utility>
#include <valarray>
#include <vector>
#include <wchar.h>

using namespace std;
typedef long long int LL;
typedef long long int ll;
typedef pair<long long int, long long int> pii;
typedef pair<double, double> pdd;

#define SORT(c) sort((c).begin(), (c).end())
#define BACKSORT(c) sort((c).begin(), (c).end(), std::greater<LL>())
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

LL mod = 998244353;

bool find(map<pair<LL, LL>, bool> &dic, LL a, LL b) {
  auto iter = dic.find(make_pair(a, b));
  return iter != end(dic);
}

bool is1(map<pair<LL, LL>, bool> &dic, LL a, LL b) {
  auto iter = dic.find(make_pair(a, b));
  return iter->second;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  LL N, M;
  cin >> N >> M;

  map<pair<LL, LL>, bool> dic;

  REP(i, M) {
    LL a, b, c;
    cin >> a >> b >> c;
    dic.insert(make_pair(make_pair(a, b), c == 1));
  }

  LL now1 = 1;
  LL now0 = 1;

  if (find(dic, 1, 1)) {
    if (is1(dic, 1, 1)) {
      now0 = 0;
    } else {
      now1 = 0;
    }
  }

  REP(i, N - 1) {
    // i+2,i+2 ->i+3,i+1
    bool flag1 = true;
    bool flag0 = true;
    LL next1 = 0;
    LL next0 = 0;
    if (find(dic, i + 3, i + 1) && find(dic, i + 1, i + 3)) {
      if ((is1(dic, i + 3, i + 1) && is1(dic, i + 1, i + 3)) ||
          ((!is1(dic, i + 3, i + 1)) && (!is1(dic, i + 1, i + 3)))) {
        // kanarazu 0
        flag1 = false;
      } else {
        flag0 = false;
      }
    }
    LL notuse = 0;
    LL cnt = 0;
    if (find(dic, i + 2, i + 1)) {
      if (is1(dic, i + 2, i + 1)) {
        cnt++;
      }
    } else {
      notuse++;
    }
    if (find(dic, i + 1, i + 2)) {
      if (is1(dic, i + 1, i + 2)) {
        cnt++;
      }
    } else {
      notuse++;
    }
    if (flag0) {
      if (notuse == 0) {
        if (cnt % 2 == 0) {
          next0 += now0;
        } else {
          next0 += now1;
        }
      } else {
        next0 += (now0 + now1) * notuse;
      }
    }
    if (flag1) {
      if (notuse == 0) {
        if (cnt % 2 == 0) {
          next1 += now1;
        } else {
          next1 += now0;
        }
      } else {
        next1 += (now0 + now1) * notuse;
      }
    }
    if (i != N - 2) {
      if ((!find(dic, i + 3, i + 1)) && (!find(dic, i + 1, i + 3))) {
        next1 *= 2;
        next0 *= 2;
      }
    }
    if (find(dic, i + 2, i + 2)) {
      if (is1(dic, i + 2, i + 2)) {
        next0 = 0;
      } else {
        next1 = 0;
      }
    }
    next1 %= mod;
    next0 %= mod;
    now1 = next1;
    now0 = next0;
  }

  LL sum = (now1 + now0) % mod;

  LL c = 0;

  for (auto i = dic.begin(); i != dic.end(); ++i) {
    auto key = i->first;
    if (abs(key.first - key.second) >= 3) {
      if (find(dic, key.second, key.first)) {
        if (is1(dic, key.first, key.second) !=
            is1(dic, key.second, key.first)) {
          sum = 0;
        }
        if (key.first > key.second) {
          c++;
        }
      } else {
        c++;
      }
    }
  }

  LL a = -c;

  REP(i, N - 3) { a += i + 1; }

  while (a > 0) {
    if (a > 1000) {
      a -= 1000;
      sum *= 23226277;
      sum %= mod;
    } else {
      a--;
      sum *= 2;
      sum %= mod;
    }
  }

  cout << sum << endl;
}