The Interesting Story of the 5 Sofit Letters of the Hebrew Alphabet
Four of the five “sofit” letters in Hebrew are actually older than their non-sofit counterparts: ך ן ף ץ, which all descend below the line. In other words, they were the only form the letter in all positions. They became shortened when not final just as a matter of ease of writing.
For example:
ץדקה became צדקה
There was also a mem that descended, which looked like this:
but went extinct after its shortened form emerged. Mem’s current final form ם was just a variation of מ used interchangeably in all positions, but later became associated with the end of a word, possibly because of popular use.
There is only one example of a medial ם that I know of: preserved in the Hebrew Bible in Isaiah 9:6. You can see it in the first word of the verse, which is usually written without vowels, and then the actual “correct” word is written after it in parentheses:
לםרבה (לְמַרְבֵּה) הַמִּשְׂרָה וּלְשָׁלוֹם אֵין־קֵץ עַל־כִּסֵּא דָוִד וְעַל־מַמְלַכְתּוֹ לְהָכִין אֹתָהּ וּלְסַעֲדָהּ בְּמִשְׁפָּט וּבִצְדָקָה מֵעַתָּה וְעַד־עוֹלָם קִנְאַת יהוה צְבָאוֹת תַּעֲשֶׂה־זֹּאת