Update mode.cs

there was one kind of cases (2 2 3 1 3 3 for example) that broke the linear algorithm. an update that fixed this case. more cases to be added.