Multiplicative Inverse: Test Cases Failing

Here is my code, I have made an extended euclidian function, but Testcases #2, #4 and #5 still fails

#include<iostream>
using namespace std;
#define ll long long
#define MOD 1000000007

ll ex, ey,ed;
void ExtendedEuclid(ll a, ll b){
    if(b==0){
        ex=1; ey=0; ed=a;
    }
    else{
        ExtendedEuclid(b, a%b);
        ll temp = ex;
        ex = ey;
        ey = temp-((a/b)*ey);
    }

}


int main(){
    ll n;
    cin>>n;
    ExtendedEuclid(n,MOD);
    cout<<ex<<endl;

}

Also I am not able to clearly comprehend the implementation, As I know that Extended Euclidean is used to find out the solution of equations of the form Ax+By = C where C is a multiple of divisor of A and B, so we calculate x and y from Ext. Euclid, now what I am able to understand is we are passing B as MOD and A as n and C as 1 so it becomes

nx + MODy = 1, 
hence nx =  1 - MODy
so x = (1/n)*(1-MODy)

so why is x giving us the solution of inverse of n?

Also, will this code work every-time as it is known that for this to work as GCD(n,MOD) must always be 1, and it might not be true for some cases?

@Saurabh-Kumar-1331476656958199

nx +MODy =1
(nx+MODy=1)MOD
nx+0=1(MOD)
therefore
nx=1(MOD)
hence , x is modulo inverse of n

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.