memoization for permutations