```def listperms(ll):
if len(ll)<=1: return [ll]
else:
return [ [ll[i]]+x for i in range(len(ll))
for x in listperms(ll[:i]+ll[i+1:]) ]

def permutations(n):
return [Permutation(x) for x in listperms(range(1,n+1))]

def standardization(w):
d = {}
for i in range(len(w)):
if w[i] in d: d[w[i]].append(i)
else: d[w[i]] = [i]
mm = reduce(lambda x,y:x+y, [d[z] for z in sorted(d)])
return  Permutation(map(lambda x:x+1, mm)).inverse()

def bubble_sort(ll):
mm = list(ll)
n = len(mm);  rd = []; done = False
while not done:
done = True
for i in range(n-1):
if mm[i]>mm[i+1]:
mm[i],mm[i+1] = mm[i+1],mm[i]
rd.append(i+1)
done = False
return mm, list(reversed(rd))

class Permutation(tuple):
def __new__(cls,x):
return tuple.__new__(cls,map(int,x))

def __mul__(self,y):
return Permutation([self[i-1] for i in y] )

def inverse(self):
d_inv = dict(zip(self,range(1,len(self)+1)))
return Permutation([d_inv[i+1] for i in range(len(self))])

def code(self):
return [len([x for x in self[i+1:]
if x<self[i]]) for i in range(len(self))]
def inv_num(self):
return sum(self.code())

@classmethod
def from_code(cls,cc):
res = []; n = len(cc); ll = range(1,n+1)
while ll:
res.append(ll[cc[0]]);ll.remove(ll[cc[0]]); cc=cc[1:]
return Permutation(res)

def descents(self):
return [i+1 for i in range(len(self)-1) if self[i]>self[i+1]]

def des_num(self):
return len(self.descents())

def maj(self):
return sum(self.descents())

def imaj(self):
return sum(self.inverse().descents())

def reduced_dec(self):
return bubble_sort(self.w)[1]

@classmethod
def from_reduced_dec(cls,rd,n):
ll = range(1,n+1)
for i in rd: ll[i-1],ll[i] = ll[i],ll[i-1]
return Permutation(ll)

def __call__(self,i):
return self[i]

def cycles(self):
cc = []; not_seen = range(1,len(self)+1)
while not_seen:
c = []; i = not_seen[0]
while i not in c:
not_seen.remove(i)
c.append(i); i=self(i)
cc.append(c)
return cc

@classmethod
def from_cycles(cls,cc):
d = {}
for c in cc:
r = len(c)
e ={c[i]:c[(i+1) % r] for i in range(r)}
d.update(e)
return Permutation([d[i] for i in sorted(d)])

if __name__ == '__main__':
S4 = permutations(4)
ll = [x.inv_num() for x in S4]
mm = [x.maj() for x in S4]
print sorted(ll)
print sorted(mm)
c = [4,2,0,1,1,0]
u = Permutation.from_code(c)
print u
v = u.inverse()
print v
print u*v
print c
print u.code()
print u.inv_num()
print u.descents()
print u.maj()

```