#include <bits/stdc++.h>
using namespace std;
long long fact[1000005], po3[1000005];
long long my_pow(long long x, long long p) {
  long long rez = 1;
  while (p > 0) {
    if (p % 2 == 1) rez = (rez * x) % 998244353;
    x = (x * x) % 998244353;
    p /= 2;
  }
  return rez;
}
long long comb(long long k, long long n) {
  if (k == 0 || k == n) return 1;
  long long rez = fact[n];
  rez = (rez * my_pow(fact[k], 998244353 - 2));
  rez = rez % 998244353;
  rez = (rez * my_pow(fact[n - k], 998244353 - 2)) % 998244353;
  return rez;
}
int main() {
  long long n, i, ans, act;
  cin >> n;
  po3[0] = fact[0] = 1;
  for (i = 1; i <= n; i++) {
    po3[i] = (po3[i - 1] * 3) % 998244353;
    fact[i] = (fact[i - 1] * i) % 998244353;
  }
  ans = 0;
  for (i = 1; i <= n; i++) {
    act = (comb(i, n) * po3[i]) % 998244353;
    act = (act * my_pow(3, n * (n - i))) % 998244353;
    if (i % 2 == 0) act = -act;
    ans = (ans + 2 * act) % 998244353;
  }
  for (i = 0; i < n; i++) {
    act = (my_pow(1 - po3[i], n) - my_pow(-po3[i], n)) % 998244353;
    act = act * comb(i, n);
    if (i % 2 == 0) act = -act;
    ans = (ans + 3 * act) % 998244353;
  }
  while (ans < 0) ans += 998244353;
  cout << ans % 998244353 << '\n';
  return 0;
}
