Problem p02958 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define test()                                                                 \
  ull t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define pb push_back
#define mkp make_pair
#define nl cout << endl
#define MOD 1000000007
#define loop(i, start, end) for (int i = start; i < end; i++)
#define N 100001
#define oa(a, n)                                                               \
  for (int i = 0; i < n; i++)                                                  \
    cout << a[i] << " ";                                                       \
  nl;
#define ov(a)                                                                  \
  for (int i = 0; i < a.size(); i++)                                           \
    cout << a[i] << endl;
#define pi M_PI
int main() {
  fastio();
#ifndef ONLINE_JUDGE
  auto freopenvar = freopen("in.txt", "r", stdin);
  auto freeopenvar2 = freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  int a[n];
  loop(i, 0, n) { cin >> a[i]; }
  int flag2 = 0;
  loop(i, 0, n) {
    if (a[i] != i + 1)
      flag2 = 1;
  }
  int flag1 = 0, flag = 0;
  loop(i, 0, n) {
    if (flag1 == 1)
      break;
    loop(j, 0, n) {
      if (i != j) {
        swap(a[i], a[j]);
        flag = 0;
        loop(k, 0, n - 1) {
          if (a[k + 1] <= a[k]) {
            flag = 1;
            break;
          }
        }
        swap(a[i], a[j]);
        if (flag == 0) {
          flag1 = 1;
          // cout<<i<<" "<<j;
          break;
        }
      }
    }
  }
  if (flag1 == 1 || flag2 == 0)
    cout << "YES" << endl;
  else
    cout << "NO";

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define test()                                                                 \
  ull t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define pb push_back
#define mkp make_pair
#define nl cout << endl
#define MOD 1000000007
#define loop(i, start, end) for (int i = start; i < end; i++)
#define N 100001
#define oa(a, n)                                                               \
  for (int i = 0; i < n; i++)                                                  \
    cout << a[i] << " ";                                                       \
  nl;
#define ov(a)                                                                  \
  for (int i = 0; i < a.size(); i++)                                           \
    cout << a[i] << endl;
#define pi M_PI
int main() {
  fastio();
  int n;
  cin >> n;
  int a[n];
  loop(i, 0, n) { cin >> a[i]; }
  int flag2 = 0;
  loop(i, 0, n) {
    if (a[i] != i + 1)
      flag2 = 1;
  }
  int flag1 = 0, flag = 0;
  loop(i, 0, n) {
    if (flag1 == 1)
      break;
    loop(j, 0, n) {
      if (i != j) {
        swap(a[i], a[j]);
        flag = 0;
        loop(k, 0, n - 1) {
          if (a[k + 1] <= a[k]) {
            flag = 1;
            break;
          }
        }
        swap(a[i], a[j]);
        if (flag == 0) {
          flag1 = 1;
          // cout<<i<<" "<<j;
          break;
        }
      }
    }
  }
  if (flag1 == 1 || flag2 == 0)
    cout << "YES" << endl;
  else
    cout << "NO";

  return 0;
}