Putting Numbers
Difficulty: Hard

My little brothers David and Goliath are learning to count up to large numbers, then they are playing a game that consist to put all the numbers from 1 to N in a table. The numbers are plastic and are in two boxes, they take it out and put it on the table in a straight line in ascending order. A box has numbers from 1 to N of a color and the other box has the numbers from 1 to N of another color. So that David and Goliath can choose a different color.
David is a smart kid and comes up with a variation of the game, he says that if each player can only put groups of consecutive numbers of a fixed size before, then there will be fewer different ways to put all the numbers from 1 to N. A way of put all numbers from 1 to N is different to another if exists a same number with different color.
Then to test the intellect of David, we ask you help calculating how many different ways to put the numbers from 1 to N if David can put sets of size k with consecutive numbers and Goliath sets of size p with consecutive numbers, but with very large numbers. They can take turns at whim.

Input Format:
The only line of input contains three integers N, p and k (2000 <= N <= 10^9) (1000 <= p,k < N) with k != p.

Output Format:
Print a line with the number if different ways to put the numbers from 1 to N modulo 10^9 + 7 (1000000007).

Sample Input:
6 2 3

Sample Output:
2

Python 3 starter code:


Reference solution (C++):
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
#define N 1000005
#define ll long long
const ll mod = 1000000007;

ll fact[N];
ll GCDext(ll a, ll b, ll &x, ll &y){
	ll g = a; x = 1; y = 0;
	if(b!=0){
		g = GCDext(b, a%b, y, x);
		y -= (a/b)*x;
	}
	return g;
}
ll invMod(ll a, ll m, ll &inv){
	ll x,y;
	if(GCDext(a,m,x,y) != 1)
		return 0;
	inv = (x+m)%m;
	return 1;
}
ll n, k, p;


int main(){

	fact[0] = 1ULL;
	for(int i = 1; i <= 1000000; ++i)
		fact[i] = (fact[i-1] * (ll)i)%mod;

	cin >> n >> p >> k;
	if(p < k) swap(p,k);
	ll aux = 0, ct = 0, ct2 = 0, sol = 0, inv, sol2;
	while(aux <= n){
		sol2 = 0;
		if((n - aux) % k == 0){
			ct2 = (n - aux) / k;
			sol2 = fact[ct+ct2];
			inv = 0;
			invMod((fact[ct2]*fact[ct])%mod, mod, inv);
			sol2 *= inv;
			sol2 %= mod;
		}
		sol += sol2;
		ct++;
		aux += p;
	}
	cout << sol%mod << endl;

	return 0;
}