Problem p03612 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
using namespace std;
/*階乗*/
ll factorial(ll n) {
  if (n == 0) {
    return 1;
  } else {
    return n * factorial(n - 1);
  }
}

/* ユークリッドの互除法 最大公約数*/
/*ll gcd(ll a, ll b) {
        ll r;
        r = a % b;
        while (r != 0) {
                a = b;
                b = r;
                r = a % b;
        }
        return b;
}
*/

/*最小公倍数*/
/*
ll lcm(ll a,ll b){
        return a/gcd(a,b)*b;
}
*/

/*二点間の距離*/
double dist(pair<double, double> a, pair<double, double> b) {
  return sqrt(pow((a.first - b.first), 2) + pow((a.second - b.second), 2));
}

// 繰り返し自乗法
double ism(double aa, ll p) {
  double ap = aa;
  double ans = 1;
  while (p > 0) {
    // cout<<"p="<<p<<",ap="<<ap<<endl;
    if (p & 1) { // 奇数が真
      ans *= ap;
    }
    p /= 2;
    ap = ap * ap;
  }
  return ans;
}
// 繰り返し自乗法(アマリトリバージョン)
ll ismm(ll aa, ll p, ll m) {
  ll ap = aa;
  ll ans = 1;
  while (p > 0) {
    // cout<<"p="<<p<<",ap="<<ap<<endl;
    if (p & 1) { // 奇数が真
      ans = (ans * ap) % m;
    }
    p /= 2;
    ap = (ap * ap) % m;
  }
  return ans;
}
/*
ll solve(ll i,ll m,vector<ll>t,ll n){
    if(m==0){
        return 1;
    }
    if(i>=n){
        return 0;
    }


    cout<<i+1<<" "<<m<<endl;
    cout<<i+1<<" "<<m-t[i]<<endl;
    ll res=solve(i+1,m,t,n)||solve(i+1,m-t[i],t,n);
    return res;
}
*/

// 小数点12桁
struct all_init {
  all_init() { cout << fixed << setprecision(12); }
} All_init;

int main() {
  ll n, count = 0, count2 = 0;
  cin >> n;
  vector<ll> p(n);
  for (ll i = 0; i < n; i++) {
    cin >> p[i];
  }
  for (ll i = 0; i < n; i++) {
    if (i + 1 == p[i]) {
      swap(p[i], p[i + 1]);
      count++;
    } else if (i == n - 1 && i + 1 == p[i]) {
      swap(p[i], p[i - 1]);
      count++;
    }
  }

  cout << count << endl;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define ll long long
using namespace std;
/*階乗*/
ll factorial(ll n) {
  if (n == 0) {
    return 1;
  } else {
    return n * factorial(n - 1);
  }
}

/* ユークリッドの互除法 最大公約数*/
/*ll gcd(ll a, ll b) {
        ll r;
        r = a % b;
        while (r != 0) {
                a = b;
                b = r;
                r = a % b;
        }
        return b;
}
*/

/*最小公倍数*/
/*
ll lcm(ll a,ll b){
        return a/gcd(a,b)*b;
}
*/

/*二点間の距離*/
double dist(pair<double, double> a, pair<double, double> b) {
  return sqrt(pow((a.first - b.first), 2) + pow((a.second - b.second), 2));
}

// 繰り返し自乗法
double ism(double aa, ll p) {
  double ap = aa;
  double ans = 1;
  while (p > 0) {
    // cout<<"p="<<p<<",ap="<<ap<<endl;
    if (p & 1) { // 奇数が真
      ans *= ap;
    }
    p /= 2;
    ap = ap * ap;
  }
  return ans;
}
// 繰り返し自乗法(アマリトリバージョン)
ll ismm(ll aa, ll p, ll m) {
  ll ap = aa;
  ll ans = 1;
  while (p > 0) {
    // cout<<"p="<<p<<",ap="<<ap<<endl;
    if (p & 1) { // 奇数が真
      ans = (ans * ap) % m;
    }
    p /= 2;
    ap = (ap * ap) % m;
  }
  return ans;
}
/*
ll solve(ll i,ll m,vector<ll>t,ll n){
    if(m==0){
        return 1;
    }
    if(i>=n){
        return 0;
    }


    cout<<i+1<<" "<<m<<endl;
    cout<<i+1<<" "<<m-t[i]<<endl;
    ll res=solve(i+1,m,t,n)||solve(i+1,m-t[i],t,n);
    return res;
}
*/

// 小数点12桁
struct all_init {
  all_init() { cout << fixed << setprecision(12); }
} All_init;

int main() {
  ll n, count = 0, count2 = 0;
  cin >> n;
  vector<ll> p(n);
  for (ll i = 0; i < n; i++) {
    cin >> p[i];
  }
  for (ll i = 0; i < n; i++) {
    if (i + 1 == p[i]) {
      if (i != n - 1) {
        swap(p[i], p[i + 1]);
      } else {
        swap(p[i], p[i - 1]);
      }
      count++;
    }
  }

  cout << count << endl;
}