Hi,
First, a question about the SubCategory - are they the same as their Category parents? If so, you can make a pivot table with parent | child many-to-many relationship. In the left side you would set the parent id (whichever it may be, either the Category or a SubCategory) and on the right you would set all its "child" categories (again, being a Category or SubCategory).
With that pivot in place, you can flag Categories as parent = true|false or top_level = true|false if you need that functionallity to distinguish the main Categories from the rest.
TL/DR - Squish SubCategories to Categories, flag them as top_level (true|false) and add a pivot many-to-many Categories to itself
Best regards,
Aleksandar