Module Module1
    Function Factorial(ByVal x As Integer) As Long
        If x = 0 Then
            Return 1
        Else
            Return x * Factorial(x - 1)
        End If
    End Function
    Function number(ByVal n As Integer) As Integer
        Dim sum As Integer = 1
        Dim answer As Integer
        For i = 1 To n - 1
            For j As Integer = 1 To i
                sum *= (n - 1)
            Next j
            answer += sum
            sum = 1
        Next i
        Return answer
    End Function
    Sub Main()
        Console.WriteLine(" Введите длину перестановки")
        Dim length As Integer = Integer.Parse(Console.ReadLine())
        Dim BigArray(length - 1, number(length - 1))
        Dim Medium(length - 1) As Integer
        For i As Integer = 0 To length - 1
            BigArray(i, 0) = i + 1
        Next i
        Dim Array(length - 1, length - 2)
        Dim n As Integer
        For i As Integer = 0 To length - 2
            If i = 0 Then
                For j As Integer = 0 To length - 1
                    Medium(j) = BigArray(j, i)
                Next j
            Else
                For j As Integer = 0 To length - 1
                    Medium(j) = Array(j, i - 1)
                Next j
            End If

            Medium(i) += Medium(i + 1)
            Medium(i + 1) = Medium(i) - Medium(i + 1)
            Medium(i) = Medium(i + 1) - Medium(i)

            For j As Integer = 0 To length - 1
                Array(j, i) = Medium(j)
                BigArray(j, i) = Medium(j)
            Next j


        Next i
    End Sub

End Module
