The psalmist either had a short sighted view of God, an over-inflated sense of his own righteousness (and that of his people), a limited perspective, all of the above, some of the above mixed together, or an insight into how things are in the real world -- that both the righteous and the unrighteous suffer.
Some suffering comes inevitably as a natural consequence of making poor choices.
Some suffering comes because we make right choices. Martin Luther King called that redemptive.
Some suffering comes because we all breath common air or because other people make poor choices in a world where we breath common air, or because we wore purple on Friday or for some other random or intentional purpose.
Some suffering comes to shape, better, or enrich us.
In any event, we all suffer and no one theological, philosophical, biblical, moral, or scientific explanation can account for all of it.
The psalmist makes a stab at it. It would seem that sometimes it is better to create and recite poetry and music than to attempt a trite answer. This falls in the sphere and atmosphere of art and prayer.
" All this has come upon us,
though we have not forgotten you,
and we have not been false to your covenant.
Our heart has not turned back,
nor have our steps departed from your way;
yet you have broken us in the place of jackals
and covered us with the shadow of death.
If we had forgotten the name of our God
or spread out our hands to a foreign god,
would not God discover this?
For he knows the secrets of the heart.
Yet for your sake we are killed all the day long;
we are regarded as sheep to be slaughtered."
(Psalm 44:17-22 ESV)
So ... take some time to listen, sing, and pray.