#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000010;
const long long MOD = 998244353;
static long long N, ans, Cn[MAXN], P3[MAXN];
inline long long qpow(long long a, long long b) {
  long long ans = 1;
  a %= MOD;
  for (; b; b >>= 1, a = a * a % MOD)
    if (b & 1) ans = ans * a % MOD;
  return ans;
}
inline long long sign(int x) { return (x & 1) ? -1 : 1; }
int main() {
  scanf("%lld", &N);
  Cn[0] = P3[0] = 1;
  for (int i = 1; i <= N; i++) {
    Cn[i] = Cn[i - 1] * (N - i + 1) % MOD * qpow(i, MOD - 2) % MOD;
    P3[i] = P3[i - 1] * 3 % MOD;
  }
  for (int i = 1; i <= N; i++) {
    (ans += sign(i + 1) * Cn[i] *
            ((qpow(P3[N - i] - 1, N) - qpow(3, N * (N - i)) + MOD) % MOD) %
            MOD) %= MOD;
    if (ans < 0) ans += MOD;
  }
  (ans *= 3) %= MOD;
  for (int i = 1; i <= N; i++) {
    (ans += 2 * sign(i + 1) * Cn[i] % MOD * P3[i] % MOD * qpow(3, N * (N - i)) %
            MOD) %= MOD;
    if (ans < 0) ans += MOD;
  }
  printf("%lld\n", ans);
  return 0;
}
