#include <bits/stdc++.h>
using namespace std;
const long long P = 998244353;
long long binom[1000005];
long long stor[1000005];
long long exp(long long a, long long n) {
  if (n == 0) return 1;
  long long b = exp(a, n / 2);
  if (n % 2) {
    return (((b * b) % P) * a) % P;
  }
  return ((b * b) % P);
}
long long inv(long long a) { return exp(a, P - 2); }
long long modp(long long a) {
  if (a > 0)
    return a % P;
  else
    return (P - (-a) % P) % P;
}
int32_t main() {
  long long ans = 0;
  long long n;
  cin >> n;
  binom[0] = 1;
  stor[0] = 1;
  for (long long i = 1; i <= n; i++) {
    binom[i] = (binom[i - 1] * (n - i + 1)) % P;
    binom[i] *= inv(i);
    binom[i] %= P;
    stor[i] = stor[i - 1] * 3;
    stor[i] %= P;
  }
  long long p, q, r, s;
  for (long long i = 1; i <= n; i++) {
    if (i % 2 == 0)
      p = P - 1;
    else
      p = 1;
    q = exp(stor[n - i], n);
    q *= stor[i];
    q %= P;
    q *= binom[i];
    q %= P;
    ans += 2 * ((p)*q) % P;
    ans %= P;
  }
  for (long long i = 1; i <= n; i++) {
    p = exp(3, P - 1 + i - n);
    p = (P - p);
    q = exp(1 + p, n);
    q += (P - 1);
    q %= P;
    r = 3 * q;
    r %= P;
    r *= exp(stor[n - i], n);
    r %= P;
    r *= binom[i];
    r %= P;
    if (i % 2 == 0)
      ans -= r;
    else
      ans += r;
    ans = modp(ans);
  }
  cout << ans << endl;
  return 0;
}
