Count Permutations

My code shows TLE error Time limit exceeded. Can anyone please optimize it

Code link: https://ide.codingblocks.com/s/193843
Question link: https://hack.codingblocks.com/app/contests/1243/435/problem