Problem p03848 (C++)

Buggy submission (status: Runtime Error):
// #includes {{{
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
// }}}
// pre-written code {{{
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define RREP(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define LET(x, a) __typeof(a) x(a)
// #define IFOR(i,it,c)
// for(__typeof((c).begin())it=(c).begin();it!=(c).end();++it,++i)
#define ALL(c) (c).begin(), (c).end()
#define MP make_pair

#define EXIST(e, s) ((s).find(e) != (s).end())

#define RESET(a) memset((a), 0, sizeof(a))
#define SET(a) memset((a), -1, sizeof(a))
#define PB push_back
#define DEC(it, command) __typeof(command) it = command

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;
#define debug2(x)                                                              \
  cerr << #x << " = [";                                                        \
  REP(__ind, (x).size()) { cerr << (x)[__ind] << ", "; }                       \
  cerr << "] (L" << __LINE__ << ")" << endl;

const int INF = 0x3f3f3f3f;

typedef long long Int;
typedef unsigned long long uInt;
#ifdef __MINGW32__
typedef double rn;
#else
typedef long double rn;
#endif

typedef pair<int, int> pii;

/*
#ifdef MYDEBUG
#include"debug.h"
#include"print.h"
#endif
*/
// }}}

typedef int Num;
const int mod = 1000000007;

//{{{ operation
inline Num prod(const Num &a, const Num &b, const Num m = mod) {
  Num c = (a * (long long)b) % m;
  //	assert(0<=c and c<m);
  return c;
}
inline void mult(Num &a, const Num &b, const Num m = mod) { a = prod(a, b, m); }
inline Num sum(const Num &a, const Num &b, const Num m = mod) {
  Num c = a + b;
  if (c >= m)
    c -= m;
  //	assert(0<=c and c<m);
  return c;
}
inline Num add(Num &a, const Num &b, const Num m = mod) { a = sum(a, b, m); }
inline Num neg(const Num &a, const Num m = mod) {
  if (a == 0)
    return 0;
  else
    return m - a;
}
inline Num diff(const Num &a, const Num &b, const Num m = mod) {
  Num c = a - b;
  if (c < 0)
    c += m;
  return c;
}
inline Num subt(Num &a, const Num &b, const Num m = mod) {
  a -= b;
  if (a < 0)
    a += m;
}
/*
inline Num inv(const Num &a, const Num m = mod){
        return invMod(a,m);
}
inline Num quot(const Num &a, const Num &b, const Num m = mod){
        return prod(a,inv(b,m),m);
}
inline void divide(Num &a, const Num &b, const Num m = mod){
        a = prod(a,inv(b,m),m);
}
*/
//}}}
int N;
int A[100010];
pii p[100010];

int main() {
  cin >> N;
  REP(i, N) cin >> A[i];
  REP(i, N) {
    int s = A[i] + N - 1;
    if (s % 2 != 0) {
      cout << 0 << endl;
      return 0;
    }
    p[i] = MP(s / 2, N - 1 - s / 2);
    assert(0 <= p[i].first <= N - 1);
    assert(0 <= p[i].second <= N - 1);
    if (p[i].first >= p[i].second)
      swap(p[i].first, p[i].second);
    //		cout<<p[i].first<<" "<<p[i].second<<endl;
  }
  map<int, int> mp;
  REP(i, N) { mp[p[i].first]++; }
  int ans = 1;
  for (auto it : mp) {
    if (it.first * 2 == N - 1) {
      if (it.second != 1) {
        cout << 0 << endl;
        return 0;
      }
    } else {
      if (it.second != 2) {
        cout << 0 << endl;
        return 0;
      } else {
        mult(ans, 2);
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
// #includes {{{
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
// }}}
// pre-written code {{{
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define RREP(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define LET(x, a) __typeof(a) x(a)
// #define IFOR(i,it,c)
// for(__typeof((c).begin())it=(c).begin();it!=(c).end();++it,++i)
#define ALL(c) (c).begin(), (c).end()
#define MP make_pair

#define EXIST(e, s) ((s).find(e) != (s).end())

#define RESET(a) memset((a), 0, sizeof(a))
#define SET(a) memset((a), -1, sizeof(a))
#define PB push_back
#define DEC(it, command) __typeof(command) it = command

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;
#define debug2(x)                                                              \
  cerr << #x << " = [";                                                        \
  REP(__ind, (x).size()) { cerr << (x)[__ind] << ", "; }                       \
  cerr << "] (L" << __LINE__ << ")" << endl;

const int INF = 0x3f3f3f3f;

typedef long long Int;
typedef unsigned long long uInt;
#ifdef __MINGW32__
typedef double rn;
#else
typedef long double rn;
#endif

typedef pair<int, int> pii;

/*
#ifdef MYDEBUG
#include"debug.h"
#include"print.h"
#endif
*/
// }}}

typedef int Num;
const int mod = 1000000007;

//{{{ operation
inline Num prod(const Num &a, const Num &b, const Num m = mod) {
  Num c = (a * (long long)b) % m;
  //	assert(0<=c and c<m);
  return c;
}
inline void mult(Num &a, const Num &b, const Num m = mod) { a = prod(a, b, m); }
inline Num sum(const Num &a, const Num &b, const Num m = mod) {
  Num c = a + b;
  if (c >= m)
    c -= m;
  //	assert(0<=c and c<m);
  return c;
}
inline Num add(Num &a, const Num &b, const Num m = mod) { a = sum(a, b, m); }
inline Num neg(const Num &a, const Num m = mod) {
  if (a == 0)
    return 0;
  else
    return m - a;
}
inline Num diff(const Num &a, const Num &b, const Num m = mod) {
  Num c = a - b;
  if (c < 0)
    c += m;
  return c;
}
inline Num subt(Num &a, const Num &b, const Num m = mod) {
  a -= b;
  if (a < 0)
    a += m;
}
/*
inline Num inv(const Num &a, const Num m = mod){
        return invMod(a,m);
}
inline Num quot(const Num &a, const Num &b, const Num m = mod){
        return prod(a,inv(b,m),m);
}
inline void divide(Num &a, const Num &b, const Num m = mod){
        a = prod(a,inv(b,m),m);
}
*/
//}}}
int N;
int A[100010];
pii p[100010];

int main() {
  cin >> N;
  REP(i, N) cin >> A[i];
  REP(i, N) {
    int s = A[i] + N - 1;
    if (s % 2 != 0) {
      cout << 0 << endl;
      return 0;
    }
    p[i] = MP(s / 2, N - 1 - s / 2);
    if (p[i].first >= p[i].second)
      swap(p[i].first, p[i].second);
    //		cout<<p[i].first<<" "<<p[i].second<<endl;
  }
  map<int, int> mp;
  REP(i, N) { mp[p[i].first]++; }
  int ans = 1;
  for (auto it : mp) {
    if (it.first * 2 == N - 1) {
      if (it.second != 1) {
        cout << 0 << endl;
        return 0;
      }
    } else {
      if (it.second != 2) {
        cout << 0 << endl;
        return 0;
      } else {
        mult(ans, 2);
      }
    }
  }
  cout << ans << endl;
  return 0;
}