added the code for counting invertions