Firstly, factorial is calculated every time, which takes O(n). This can be fixed by calculating it only once for all N and saving it in an array.
Secondly, removing an item from an ArrayList is O(n). If you want O(1), you must use a LinkedList to get it in O(1)
Please fix the solution.