Sum Over All Substrings Hard Version