fixed permute template function