First, for question 1, yes, the sum of the 40 shareholder's basis should equal the sum of the M-2 ending balance.
Loans from a shareholder provide debt basis. That is tracked separately from Stock basis. Debt basis will allow a shareholder to deduct a loss in excess of stock basis, which makes sense because the AAA can go negative for losses in excess of stock basis.
If you use Lacerte, you know that the Basis Schedule tracks stock basis first, and after the stock basis is calculated, the debt basis is calculated. The shareholder's total basis is the sum of these two.
There are a couple of situations that would be unique to shareholders, but not many. These would mostly included loans to the corporation, or selling assets to the corporation, which may result in some built-in gains. Otherwise, since the corporation requires all items, such as income, distributions, credits, etc., to be allocated based on share ownership, the items for one shareholder will be given to all shareholders. As I said, the only exceptions I can think of are loans to the company, and transactions
between the shareholder and the company.
I hope this helps! Let me know if you have any more questions.