All pastes #2052186 Raw Edit

Unnamed

public text v1 · immutable
#2052186 ·published 2011-04-30 11:27 UTC
rendered paste body
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <cstring>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <cassert>
#include <functional>
#include <iterator>
#include <utility>

typedef long double LD;
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;


int main()
{

#ifdef DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	LL n;
	int m;
	cin >> n >> m;

	int y = 1;
	int ans = 0;
	
	for (LL i = n; i >= max(0LL, n - m); --i)
	{
		int d = (i & 1)	? (-1) : (+1);
		int add = y * d;
		if (add < 0) add += m;
		ans += add;
		if (ans >= m) ans -= m;
		y = ((LL)y * i) % m;
	}

	cout << ((LL)ans * ans) % m << '\n';

	return 0;
}