Problem p02958 (python)

Fixed submission:
#! /usr/bin/python
# -*- encoding: utf-8 -*-

import sys

if __name__ == '__main__':
    inputN = int(input())
    inputList = list(map(int, input().split()))
    #
    flag = False
    before = 0
    for k in range(inputN):
        if before >= inputList[k]:
            flag = True
        before = inputList[k]
    
    #
    if not flag:
        sys.stdout.write("YES")
        sys.exit()
    
    #  
    for i in range(inputN):
        for j in range(inputN):
            if i != j:
                changeList = inputList[:]
                swap = changeList[i]
                changeList[i] = changeList[j]
                changeList[j] = swap
          
                #
                flag = False
                before = 0
                for k in range(inputN):
                    if before >= changeList[k]:
                        flag = True
                    before = changeList[k]
          
                #
                if not flag:
                    sys.stdout.write("YES")
                    sys.exit()


    sys.stdout.write("NO")

Buggy submission (verdict: Runtime Error):
#! /usr/bin/python
# -*- encoding: utf-8 -*-

import sys

if __name__ == '__main__':
    inputN = int(input())
    inputList = list(map(int, input().split()))
    #
    flag = False
    before = 0
    for k in range(inputN):
        if before >= inputList[k]:
            flag = True
        before = inputList[k]
    
    #
    if not flag:
        sys.stdout.write("YES")
        sys.exit()
    
    #  
    for i in range(inputN):
        for j in range(inputN):
            if i != j:
                changeList = inputList[:]
                swap = changeList[i]
                changeList[i] = changeList[j]
                changeList[j] = swap
          
                #
                flag = False
                before = 0
                for k in range(inputN):
                    if before >= changeList[k]:
                        flag = True
                    before = changeList[k]
          
                #
                if not flag:
                    sys.stdout.write("YES")
                    sys.exit()


sys.stdout.write("NO")

Buggy submission (verdict: Runtime Error):
#! /usr/bin/python
# -*- encoding: utf-8 -*-

import sys

if __name__ == '__main__':
    inputN = int(input())
    inputList = list(map(int, input().split()))
    #
    flag = False
    before = 0
    for k in range(inputN):
        if before >= inputList[k]:
            flag = True
        before = inputList[k]
    
    #
    if not flag:
        print("YES")
        sys.exit()
    
    #  
    for i in range(inputN):
        for j in range(inputN):
            if i != j:
                changeList = inputList[:]
                swap = changeList[i]
                changeList[i] = changeList[j]
                changeList[j] = swap
          
                #
                flag = False
                before = 0
                for k in range(inputN):
                    if before >= changeList[k]:
                        flag = True
                    before = changeList[k]
          
                #
                if not flag:
                    print("YES")
                    sys.exit()


print("NO")